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
SayasoniSayasoni 

Help in increasing test coverage from 66%

I would like to increase the test code coverage of the following class which currently is 66%

 

Controller:

 

public class SendEmail {

    private Attachment attachment;
    private String intvId;
    private Interview__c interview;
    private EmailTemplate eTemplate;
    private Contact con;
    private Candidates__c cand;
    private User user;

    public SendEmail(ApexPages.StandardController controller) {
         intvId = ApexPages.currentPage().getParameters().get('id');
         if (intvId == null) {
            interview = new Interview__c();
        }
        else {
        
        interview = [select Name,id,Client_Names__r.Email,Candidate_Names__c,Client_Names__c,Interview_Date__c,Interview_Time__c,Address_of_Interview__c from Interview__c where id = :intvId];
        con = [Select Id,Name,FirstName,Email from Contact where Id = :interview.Client_Names__c];
        cand = [Select Id,Name,First_Name__c from Candidates__c where Id = :interview.Candidate_Names__c];
        eTemplate = [Select id,Body,Subject,DeveloperName from EmailTemplate where DeveloperName = 'Interview_Client_Email' ];
        user = [Select Id,FirstName,LastName from User where Id = :UserInfo.getUserId()];
    }
   }

public Interview__c getInterview() {
return interview;
}
public EmailTemplate getETemplate() {
return eTemplate;
}
public Contact getCon() {
return con;
}
public Candidates__c getCand() {
return cand;
}
public User gerUser() {
return user;
}

public PageReference send() {
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

 String emailMessage = 'Dear ' +con.FirstName  +',' 
                       +'\n<br/>'+
                       +'\n<br/>'+ 'I am pleased to confirm your interview with our candidate '
                       + cand.First_Name__c +'.'
                       +'We have scheduled the interview for '
                       + interview.Interview_Date__c +' at '
                       + interview.Interview_Time__c +' at '
                       + interview.Address_of_Interview__c +'.'
                       +'\n<br/>'+ 'As I mentioned previously, it is our pleasure to have the chance '
                       + 'to work with you and I wish you the very best of luck with this candidate.'
                       +'\n<br/>'+
                       +'\n<br/>'+
                       +'Kindest regards,'
                       +'\n<br/>'+
                       +user.FirstName+' ' +user.LastName;
                       
                      
email.setTargetObjectId(con.Id);
email.setSubject('Interview Confirmation');
email.setPlainTextBody(emailMessage);
email.setHtmlBody(emailMessage);
email.setBccSender(true);

List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :interview.Candidate_Names__c and (NOT Name like 'NDA%') and(Not Name like 'P33 Consultant NDA%') ])
        
        {  // Add to attachment file list  
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();  
            efa.setFileName(a.Name); 
            efa.setBody(a.Body); 
            fileAttachments.add(efa);
        }
       email.setFileAttachments(fileAttachments);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
    
 
        
        PageReference pg = new PageReference('https://na1.salesforce.com/'+intvId);
            pg.setRedirect(true);
            
            return pg;   
    }

}



 

Test Case:

 

@istest
private class SendEmailTest {

    static testmethod void testSendEmail(){
    
        Profile p = [select id from profile where name='System Administrator'];
       User u = new User(alias = 'utest', email='unit.test@unit.test.com',
      emailencodingkey='UTF-8', lastname='Unit Test', 
      languagelocalekey='en_US',
      localesidkey='en_GB', profileid = p.Id,
      timezonesidkey='Europe/London', 
      username='unit.test@unit.test.com');
                 
        System.runAs(u)
            {
     
    
    Contact con = new Contact(
                Lastname = 'test contact',
                Email = 'test@test.com');
               
             insert con;
             
     Candidates__c cand = new Candidates__C (
        First_Name__c = 'Test Name',
        Name = 'Test Last name',
        Candidate_Address__c = 'Test Address',
        Main_Email__c = 'test@test1.com',
        Candidate_Source__c = 'Test Source');
        
        insert cand;
        
       
     String folder = [select id,name from Folder where type='Email'][0].id;
     
     EmailTemplate eTemp = new EmailTemplate (
            Subject = 'Test Subject',
            Body = 'Test Body',
            folderId = folder,
            TemplateType = 'Text',
            Name = 'Test Name',
            DeveloperName = 'test');
        
        insert eTemp;
        
     Interview__c intv = new Interview__c (
         Client_Names__c = con.Id,
         Candidate_Names__c = cand.Id,
         Interview_Date__c = Date.today(),
         Interview_Time__c = 'time',
         Address_of_Interview__c = 'test address');
         
         insert intv;       
   
       
        ApexPages.StandardController controller = new ApexPages.StandardController(intv);
    
        ApexPages.currentPage().getParameters().put('id' , intv.id);
                    
        //Create instance of controller   
        SendEmail testSend = new SendEmail(controller);  
       
        test.startTest();  
         
        //call the method that you want to test using instance of class  
        
        PageReference pg =  testSend.Send();
        
        List<Interview__c> intrv = [SELECT ID, Name,Client_Names__c
                FROM Interview__c 
                WHERE ID = :intv.Id];
                
        System.assertEquals(intv.Client_Names__c,intrv[0].Client_Names__c);
           
       
          
        test.stopTest();  
  
       }
      }
     }



 

Best Answer chosen by Admin (Salesforce Developers) 
SayaSaya

I managed to push it to 75%,thats good enough.