+ Start a Discussion
SayasoniSayasoni 

Help raise my test coverage from 56%

I have a controller that sends notications a month before a candidate's birthday. I can't seem to cover the else part of my if statements in my test class.The red parts being the ones not covered.

Kindly assist:

 

public class candidateEmailNotification{

public List<Candidates__c> candidates = [select First_Name__c,Name,Date_of_Birth__c
                         from Candidates__c];
       
 
       public List<Candidates__c> includeInEmail(){  
            
              List<Candidates__c> newcadidates = new  List<Candidates__c>(); 
              for(Candidates__c c: candidates) {

                  
                    if(c.Date_of_Birth__c==null){
                    
                    }
               else{
                    
              
               if(c.Date_of_Birth__c.dayOfYear() - System.today().dayOfYear()==30){

               
               newcadidates.add(c);
             
               }
               
           }
          } 

          return newcadidates;
        } 
        
        
        
     public String composeMessage(){
     
        
       String emailMsg = '';
       List<Candidates__c> c = includeInEmail();

       if(includeInEmail().size()==0){
       return null;
          }else{
       for(Integer i = 0;i< c.size(); i++){ emailMsg += '<tr><td>'+c.get(i).First_Name__c +'</td><td>'+c.get(i).Name+'</td><td style = color:red> '+c.get(i).Date_of_Birth__c.day()+ ' - '+c.get(i).Date_of_Birth__c.month() +' - '+System.today().year()+'</td></tr>';
       
    
       }
      
       
        } 
         
       return '<table><tr><th>First Name </th><th>Last Name</th><th>Birthday</th></tr>'+emailMsg+'</table>';
        }           
  
    public void sendMail() {
         if(composeMessage()==null){
         
         }else{
 
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {'martin.mathu@jjpeople.com'}; mail.setToAddresses(toAddresses); mail.setSubject(' Birthday Notification'); mail.setUseSignature(false); mail.setHtmlBody('<div style=color:green;><u>The Following Candidate(s) Have Birthday in one Month Time</u></div><br>' + composeMessage()); // Send the email Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
    }
    }

}

 Below is my test class:

@isTest
public class CandidateEmailNotificationTest {
    static testMethod void EmailNotificationTest() {
        List<Candidates__c> cand =  new List<Candidates__c>();
        Candidates__c candidate = new Candidates__c(Candidate_Source__c='Monster',Main_Email__c='emaill@email.com',Candidate_Mobile_Phone__c='000',Name ='LastN',First_Name__c = 'Tester',Candidate_Address__c='000',Date_of_Birth__c = System.today());
        Candidates__c candidate2 = new Candidates__c(Candidate_Source__c='test',Main_Email__c='emaill2@email.com',Candidate_Mobile_Phone__c='0011',Name ='LastN2',First_Name__c = 'Tester2',Candidate_Address__c='00022',Date_of_Birth__c = System.today());
        cand.add(candidate);
        cand.add(candidate2);
        insert cand;        
        
        Test.startTest();
        candidateEmailNotification cn =  new candidateEmailNotification();
        cn.candidates = cand;
        cn.includeInEmail();
        cn.composeMessage();
        cn.sendMail();       
        Test.stopTest();
                
        System.assertEquals(candidate.Date_of_Birth__c, System.today());
        }

}

 

Best Answer chosen by Admin (Salesforce Developers) 
NjangaNjanga

Ok this is simple.

Your newCandidate list is 0 size because u do not have a candidate who has birthday in the next 30 days and so the sendEmail method it is returning null and therefore the rest of method body is not covered.

Therefore you need to add new candidate with atleast 30days before birthday.

So!

Set  date_of_birth =(Syestem.today().addDays(30))

and in assertEquals(date_of_birth,(Syestem.today().addDays(30)))

All Answers

Suresh RaghuramSuresh Raghuram

can u try like this

change the condition and bring the else part into if and if part into else.

 

more Chances are there to ger covered except for loop part (less chance).

 

If this answers your question make this as a solution.

AshlekhAshlekh

you can write your function includeEmail() in this way also :-

public List<Candidates__c> includeInEmail()
 {  
      List<Candidates__c> newcadidates = new  List<Candidates__c>();
      for(Candidates__c c: candidates)
      {
          if(c.Date_of_Birth__c!=null && (c.Date_of_Birth__c.dayOfYear() - System.today().dayOfYear())==30)
          {
                        newcadidates.add(c);
          }
         }  
     return newcadidates;
}

 

And in your test class you are creating data for testing than you are inserting  in date of birth  =system.today() that so your controller will not reach in the if condation of includeEamil(); so you have to insert that date which is 30 day privious from today then your if condaiton will executed and your code will be cover .

I think it will help you

 

sandeep@Salesforcesandeep@Salesforce

public class candidateEmailNotification
{

public List<Candidates__c> candidates ;
public candidateEmailNotification()
{
candidates = [select First_Name__c,Name,Date_of_Birth__c from Candidates__c];
}

public List<Candidates__c> includeInEmail()
{
List<Candidates__c> newcadidates = new List<Candidates__c>();
for(Candidates__c c: candidates)
{
if(c.Date_of_Birth__c != null && c.Date_of_Birth__c.dayOfYear() - System.today().dayOfYear()== 30)
{
newcadidates.add(c);
}
}
return newcadidates;
}

public String composeMessage()
{
String emailMsg = '';
List<Candidates__c> c = includeInEmail();
if(c.size()>0)
{
for(Integer i = 0;i< c.size(); i++)
{ if(c.get(i) != null && c.get(i).Date_of_Birth__c != null)
emailMsg += '<tr><td>'+c.get(i).First_Name__c +'</td><td>'+c.get(i).Name+'</td><td style = color:red> '+c.get(i).Date_of_Birth__c.day()+ ' - '+c.get(i).Date_of_Birth__c.month() +' - '+System.today().year()+'</td></tr>';
}
}
return '<table><tr><th>First Name </th><th>Last Name</th><th>Birthday</th></tr>'+emailMsg+'</table>';
}

public void sendMail()
{
if(composeMessage!=null)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'martin.mathu@jjpeople.com'};
mail.setToAddresses(toAddresses); mail.setSubject(' Birthday Notification');
mail.setUseSignature(false);
mail.setHtmlBody('<div style=color:green;><u>The Following Candidate(s) Have Birthday in one Month Time</u></div><br>' + composeMessage()); // Send the email Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}

//===========================================

@isTest
public class CandidateEmailNotificationTest
{
static testMethod void EmailNotificationTest() {

List<Candidates__c> cand = new List<Candidates__c>();
Candidates__c candidate = new Candidates__c(Candidate_Source__c='Monster',Main_Email__c='emaill@email.com',Candidate_Mobile_Phone__c='000',Name ='LastN',First_Name__c = 'Tester',Candidate_Address__c='000',Date_of_Birth__c = System.today()-40);
Candidates__c candidate2 = new Candidates__c(Candidate_Source__c='test',Main_Email__c='emaill2@email.com',Candidate_Mobile_Phone__c='0011',Name ='LastN2',First_Name__c = 'Tester2',Candidate_Address__c='00022',Date_of_Birth__c = System.today());
cand.add(candidate);
cand.add(candidate2);
insert cand;

Test.startTest();
candidateEmailNotification cn = new candidateEmailNotification();
//cn.candidates = cand;
cn.includeInEmail();
cn.composeMessage();
cn.sendMail();
Test.stopTest();

System.assertEquals(candidate.Date_of_Birth__c, System.today());
}

}

 

This will give 100%

NjangaNjanga

Ok this is simple.

Your newCandidate list is 0 size because u do not have a candidate who has birthday in the next 30 days and so the sendEmail method it is returning null and therefore the rest of method body is not covered.

Therefore you need to add new candidate with atleast 30days before birthday.

So!

Set  date_of_birth =(Syestem.today().addDays(30))

and in assertEquals(date_of_birth,(Syestem.today().addDays(30)))

This was selected as the best answer
SayasoniSayasoni

Thanks guys for your suggestions.They pointed me to the right direction especially Njaga's and i finally managed to rise it to 96%.

amit_sfdcT-2amit_sfdcT-2

Glad to know that your issue has been resolved.

 

Please mark this as closed.

 

Regards,

Amit Kumar Gupta

Salesforce.com