function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rakiraki 

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

error for these red lines plz help me to sort it out

 

 

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 );

}
}

ManjunathManjunath

Hi,

 

Remove the above commented line and change the TestBank.branch_Owner__c to some other id (Not the currentOwnerId).
EX:
String currentOwnerId = UserInfo.getUserId();   (Let it remain same)
TestBank.branch_Owner__c = 'Some other user id'; (Change the highligted text with the other user id )

This should do it.

Regards,

Manjunath

asish1989asish1989

Hi

  Remove that comment... and add some line like this...In your test class

    

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId (currentOwnerId);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 

 

Did this post Answers your questions ...if so please mark it as solved..

 

    Thanks

rakiraki

Hi Manjunath thanks for the reply, I changed the to some thing like '00790000001wKVj' but there is no change in it 

rakiraki

Hi Asish, I tried the code but there is not in it . I wrote the trigger in class class coverage is showing as 63% but trigger coverage is showing as 100%

ManjunathManjunath

Hi,

 

Before inserting the TestBank add email to this object in your test class.

 

Regards,

Manjunath

rakiraki

In this case i getting the error as 

 

 Invalid field Email for SObject Bank__c

ManjunathManjunath

Hi,

 

Try this TestBank.bank_Owner__r.email='abc@abc.com';

 

Regards,

Manjunath

rakiraki

Hi,

 

Try this TestBank.bank_Owner__r.email='abc@abc.com';

 

its not working 0%

 

sfdcfoxsfdcfox

I'd be more worried about the fact that your code isn't bulkified rather than a 7% coverage that might not even be necessary once you fix your code.

ManjunathManjunath

Hi,

 

You have a lookup relationship with your bank_Owner__c object . First create a record for bank_Owner__c object (with all the details which is being used in TestBank). Insert that then use the previous code posted.