Find your content:

Search form

You are here

Test Coverage 93% how to Make this code cover to 100%

public class TriggerClass {
public void Mycodecoverage(Branch__c Mycodecoverage)
string BankId = Mycodecoverage.Project_Bank__c;
List<Bank__c> bank = [select id,project__c,,bank_Description__c from Bank__c where id =: BankId];

string BankName = bank[0];
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, from Bank__c where id =: BankId];
String bankOwnerEmail = '';
String bankOwnerId = '';

if(banklist.size()>0 && banklist[0].bank_Owner__c != null && banklist[0] != null)
bankOwnerEmail = banklist[0];
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>();

if(bankOwnerId != currentOwnerId){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = Emails;
String Esubject = bankDescription + '/' + BankName;
mail.setTargetObjectId (UserInfo.getUserId());
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

private static void TriggerClass()
trg_TriggerClass branchClass= new TriggerClass ();

Account account = new Account(); = 'test account';
insert account;
catch(exception e){}
system.assertnotequals(account ,null);

Project__c project = new Project__c(); = 'test project';
project.Account__c =;
insert project;
catch(exception e){}

//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 )
insert TestBank;
catch(exception e){}

Branch__C branch= new Branch__C();
branch.Project_Bank__c= TestBank.Id;
branch.Make_Public__c = true ;
branch.Comment__c = '';
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:

apex classes

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.

class description

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

My Block Status

My Block Content