You need to sign in to do that
Don't have an account?
How to write code coverage for if statements in test method
Please guide me for getting full code coverage for the following class by using best practices,
public class sendemail_torelated_contacts
{
list<string>emailids;
public PageReference sendemail()
{
emailids=new list<string>();
for(contact c:conlst)
{
if(c.email!=null)
{
emailids.add(c.email);
}
}
system.debug('-----------------------------------------------'+emailids.size());
if(emailids.size()>0)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(emailids);
mail.setSubject('Test Mail');
mail.setUseSignature(false);
mail.setHtmlBody('This is a test mail');
// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, 'Mail sent successfully');
apexpages.addmessage(myMsg);
}else
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.warning, 'NO Destination found');
apexpages.addmessage(myMsg);
}
return null;
}
public PageReference find_contacts()
{
//system.debug('=================================='+accid);
conlst=new contact[]{};
if(accid!='')
{
conlst=[select id,accountid,name,email,phone from contact where accountid=:accid];
}
if(conlst.size()>0)
{
showbtn=true;
}else
{
showbtn=false;
}
return null;
}
public String accid { get; set; }
public boolean showbtn{get;set;}
account[]acclst;
public contact[]conlst{get;set;}
public account[] getaccounts()
{
acclst=new account[]{};
acclst=[select id,name,phone from account where name in('appshark','ibm','infosys')];
return acclst;
}
//Test method for code coverage-----------------------
Private static testmethod void test()
{
account a=new account(name='test',phone='09030182818');
insert a;
contact con=new contact(lastname='testcon',email='vasu.takasi@test.com',accountid=a.id);
insert con;
sendemail_torelated_contacts obj=new sendemail_torelated_contacts();
obj.showbtn=true;
obj.conlst=new contact[]{};
obj.conlst.add(con);
obj.getaccounts();
obj.find_contacts();
obj.sendemail();
}
}
Just nothing is there
You have for True stmt
obj.showbtn=true;
obj.conlst=new contact[]{};
obj.conlst.add(con);
obj.getaccounts();
obj.find_contacts();
obj.sendemail();
Just write same code for false stmt and call the methods one more time.
obj.showbtn=false;
obj.conlst=new contact[]{};
obj.conlst.add(con);
obj.getaccounts();
obj.find_contacts();
obj.sendemail();
All Answers
insert another record with email ==null, and use the sendEamil function on it.
Just nothing is there
You have for True stmt
obj.showbtn=true;
obj.conlst=new contact[]{};
obj.conlst.add(con);
obj.getaccounts();
obj.find_contacts();
obj.sendemail();
Just write same code for false stmt and call the methods one more time.
obj.showbtn=false;
obj.conlst=new contact[]{};
obj.conlst.add(con);
obj.getaccounts();
obj.find_contacts();
obj.sendemail();