public class TriggerClass {
public void Mycodecoverage(Branch__c Mycodecoverage)
{
string BankId = Mycodecoverage.Project_Bank__c;
List<Bank__c> bank = [select id,project__c,project__r.name,bank_Description__c from Bank__c where id =: BankId];
string BankName = bank[0].Project__r.name;
system.debug('---> Project Name --->'+BankName);
string bankDescription = bank[0].bank_Description__c;
string MycodecoverageComment = Mycodecoverage.Comment__c;
system.debug('---> bank Update Comment --->'+MycodecoverageComment);
List<Bank__c> banklist = [select id,name,bank_Description__c,bank_Owner__c,bank_Owner__r.email from Bank__c where id =: BankId];
String bankOwnerEmail = '';
String bankOwnerId = '';
if(banklist.size()>0 && banklist[0].bank_Owner__c != null && banklist[0].bank_Owner__r.email != null)
{
bankOwnerEmail = banklist[0].bank_Owner__r.email;
bankOwnerId = banklist[0].bank_Owner__c;
}
String currentOwnerId = UserInfo.getUserId();
List<User> user = [select id,name,email from User where id =: currentOwnerId];
String currentownerEmail = user[0].Email;
List<String> Emails = new List<String>();
Emails.add(bankOwnerEmail);
if(bankOwnerId != currentOwnerId){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = Emails;
mail.setToAddresses(toAddresses);
mail.setBccSender(true);
mail.setUseSignature(false);
String Esubject = bankDescription + '/' + BankName;
mail.setSubject(Esubject);
mail.setPlainTextBody(MycodecoverageComment);
mail.saveAsActivity=false;
mail.setTargetObjectId (UserInfo.getUserId());
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
@isTest
private static void TriggerClass()
{
trg_TriggerClass branchClass= new TriggerClass ();
Account account = new Account();
account.name = 'test account';
try{
insert account;
}
catch(exception e){}
system.assertnotequals(account ,null);
Project__c project = new Project__c();
project.name = 'test project';
project.Account__c = account.id;
try{
insert project;
}
catch(exception e){}
system.assertnotequals(project,null);
//String currentOwnerId = UserInfo.getUserId(); (if i remove the comment for this )
Bank__c TestBank= new Bank__c();
TestBank.Name = 'test branch';
TestBank.Project__c = project.Id;
TestBank.branch_Description__c = 'test';
//TestBank.branch_Owner__c = currentOwnerId; (and remove comment for this then two red lines are covering but Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); to--------- Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail } is not covering )
try{
insert TestBank;
}
catch(exception e){}
system.assertnotequals(TestBank,null);
Branch__C branch= new Branch__C();
branch.Project_Bank__c= TestBank.Id;
branch.Make_Public__c = true ;
branch.Comment__c = '';
try
{
insert branch;
}
catch(exception e){}
system.assertnotequals(branch,null );
}
}
Attribution to: rakesh
Possible Suggestion/Solution #1
When you go to your apex classes in salesforce you should see the code coverage of the specific class right next to your classname:
then when you click the percentage displaying the code coverage you should see the class with each line colored depending on did the line of code got included in the code coverage, blue = yes, red = no.
this way you can perfectly see which lines are covered and which aren't.
Attribution to: pjcarly
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4444