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
Arup SarkarArup Sarkar 

Test class coverage 88%

Hi I have attached the mail controller class file, I have written a corresponding test class, I am not getting code coverage beyond 88%. The red lines are on the following lines in the main class, I would like to increase my code coverage to 100% if possible.

 

        Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
        attach.setContentType('application/pdf');
        attach.setFileName('Call Report.pdf');
        attach.setInline(false);
        attach.Body = body;        
        
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

        PageReference p = new PageReference('/' + theId);
        return p;

 Main Class:

 

public class CallReportSendEmailClass {
 
    private final Call_Report_Summary__c t;
    
    public string sTo {get;set;}
    public string sSubject {get;set;}
    public string sBody {get;set;}
    public string sBehalf {get;set;}
    Public boolean Cancel {get;set;}

    List<String> NBContacts = new String[]{};
    String SelectedContacts = '';

    public List<Contact> TaskContact = new List<Contact>();

    public String accountName {get; set;}
    public Id accountId {get; set;}
    public List<Contact> contacts {get;set;}
    public String ConEmail {get;set;}
    public String cc {get;set;}
    public String sHtmlBody = '';
    
    
    public Date From_Date{get;set;}
    public Date To_Date{get;set;}
    Public String Account{get;set;}
    Public String Contact{get;set;}
    Public String User{get;set;}
    Public String Opportunity{get;set;}
    Public String DateRangeEquality{get;set;}
    Public String DateLiteral{get;set;}
    Public String EmailContactId{get;set;}
    Public String Email{get;set;}
    Public String Subject{get;set;}
    Public String EmailBody{get;set;}    
    Public String Region{get;set;}
    Public String Channel{get;set;}
    Public String SubChannel{get;set;}    
      
      
    public Call_Report_Summary__c EmailReportParams = new Call_Report_Summary__c();      
      
    public CallReportSendEmailClass(ApexPages.StandardController stdController) {
        this.t = (Call_Report_Summary__c)stdController.getRecord();
        
    }
    
    Public PageReference CancelEmail(){
        Cancel = true;
        String theId = ApexPages.currentPage().getParameters().get('id');
        PageReference p = new PageReference('/' + theId);
        return p;        
        
    }
 
    public PageReference SendEmail() {
    
    
 
        String theId = ApexPages.currentPage().getParameters().get('id');
        
        System.debug(' Call Report Template Id : ' + theId);
        
        PageReference pdf = new PageReference('/apex/CallReportDataComponentPage' + '?id=' + theId);
                
        if (accountName == null) {accountName = 'jayakumar.vadivelkarumbayiram@nb.com';}
        List<String> toaddress = accountName.split(';');                
                
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(toaddress);
        

        
        mail.setsubject(sSubject);
        
        sHtmlBody = '<html>';
        sHtmlBody = sHtmlBody + '<head><title>Tutorial: HelloWorld</title></head><body>';
        //sHtmlBody = sHtmlBody + '<table>';
            
        if (sBody != null ) {
            sBody = sBody.replace('\n', '<br>');
        }
        sHtmlBody = sHtmlBody + sBody;
        sHtmlBody = sHtmlBody + '</body></html>';
        mail.setHtmlBody(sHtmlBody);

                
        
        Blob body;
        body = pdf.getContent();
        Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
        attach.setContentType('application/pdf');
        attach.setFileName('Call Report.pdf');
        attach.setInline(false);
        attach.Body = body;        
        
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

        PageReference p = new PageReference('/' + theId);
        return p;
    }
    
    public List<selectOption> getItems() {
        List<selectOption> options = new List<selectOption>();
        for (Contact nbcontactsall : [SELECT Email FROM Contact ORDER BY Email LIMIT 100]) {
            options.add(new selectOption(nbcontactsall.Email, nbcontactsall.Email));
        }
        return options;
    }
 
     public String getCC() {
        String cc = 'From Apex'; 
        return cc;
    }
 
     public void setCC(String s) {
        this.cc = 'From Apex'; 
    }

    public String[] getNBContacts() {
            return NBContacts;
    }
    
    public void setNBContacts(String[] NBContacts) {
            this.NBContacts = NBContacts;
    } 

    public String getSelectedContacts() {
            return SelectedContacts;
    }
    
    public PageReference findContacts()
    {
    if (null!=accountId)
    {
       contacts=[select id,FirstName, LastName from Contact where AccountId=:accountId];
    }
    
    return null;
  }
  
    //Method CheckNull: Will return true or false based on column value
    //Invoked From: InvokeCallReport
    //Return Value: Boolean
    
    //private boolean CheckNull(String ColumnValue){
    //    boolean val;
    //    if(ColumnValue == null){
    //        val = true;
    //    }else{
    //        val = false;
    //    }
    //    return val;
    //}  

}

 Test class:

 

@isTest
private class TestCallReportSendEmailClass{

    static testMethod void myUnitTest()
    {
        //Get a record from Call_Report_Summary__c object
        Call_Report_Summary__c CallReportSummaryId = new Call_Report_Summary__c();
        CallReportSummaryId = [select Id from Call_Report_Summary__c limit 1];

        System.debug(CallReportSummaryId.Id);
        PageReference ParentPage = new PageReference('/' + CallReportSummaryId.Id);
        
        //Get the Page Reference of the Call Report SendMultiple EmailPage
        PageReference pageRef = Page.CallReportSendMultipleEmailPage;
        pageRef.getParameters().put('id',CallReportSummaryId.Id);
        //Set the current Page
        Test.setCurrentPageReference(pageRef);
        
        Test.startTest();
        
        //create an instance of the standard controller
        ApexPages.StandardController sc = new ApexPages.standardController(CallReportSummaryId);
    
        //Create an instance of the Controller class
        CallReportSendEmailClass mySendEmailController = new CallReportSendEmailClass(sc);
        
        PageReference testPage1 = new PageReference('/apex/CallReportDataComponentPage' + '?id=' + CallReportSummaryId.Id);
        ApexPages.currentPage().getParameters().put('Id',CallReportSummaryId.Id);
        
        
        //validate CancelEmail Method
        PageReference pageCancelEmail = mySendEmailController.CancelEmail();
        
        //validate setCC Method
        mySendEmailController.setCC('arup.sarkar@nb.com');
        String get_cc = mySendEmailController.getCC();
        
        //validate findContacts Method
        PageReference findContacts = mySendEmailController.findContacts();        
        List<selectOption> items = mySendEmailController.getItems();
        String testContacts = mySendEmailController.getSelectedContacts();
        
        //validate setNBContacts/getNBContacts Method
        List<String> nbContacts = new String[]{'testing'};
        mySendEmailController.setNBContacts(nbContacts);
        String[] nbContacts1 = mySendEmailController.getNBContacts();            
        
        //validate sendemail Method
        mySendEmailController.sBody = 'This is a test body';
        PageReference send_email_page = mySendEmailController.SendEmail();
        System.assertEquals(send_email_page.getURL(), ParentPage.getURL());
        
        
        Test.stopTest();       
    
    }    

}

 

Arup SarkarArup Sarkar

Actually I am getting the following error.

 

System.VisualforceException: Methods defined as TestMethod do not support getContent call, test skipped

 

osamanosaman

Test methods don't support getContent method. You can put a condition on "Body" if its running test populate a dummy text and o the manipulation.

 

Blob body = !Test.isRunningTest() ? pdf.getContent() : Blob.ValueOf('dummy text');