0
global class CertificationEmail implements Database.Batchable<sObject>{
    global String soql='';

    global Database.QueryLocator start(Database.BatchableContext bc) {
       
        Date d = Date.today();
        Date expiryOne = System.today()+30;
        Date expiryTwo = System.today()+7;
        Date expiryThree = System.today()+3;
        String soql = 'SELECT Id, CreatedById,Manager_Email_id__c,Certifications_Email__c,Certification_Expired_Date__c,emp_first_name1__c,Emp_last_name2__c,certification_Names_del__c, Employee__c,Expiry_Date__c FROM Certification__c where Expiry_Date__c=:expiryOne OR Expiry_Date__c=:expiryTwo OR Expiry_Date__c=:expiryThree OR Expiry_Date__c=:d';
        
        return Database.getQueryLocator(soql);
    }
    
    global void execute(Database.BatchableContext bc, List<Certification__c> recs) {
        system.debug(recs.size());
        if(!test.isrunningtest()){   
            List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
            for(Certification__c m : recs) {
                List<String> toAddresses = new List<String>();
               
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                toAddresses.add(m.Certifications_Email__c);
                 mail.setToAddresses(toAddresses);
                 list<string> ccaddress = new list<string>();
                ccaddress.add(m.Manager_Email_id__c);
                mail.setCcAddresses(ccaddress);
                mail.setSubject('Certificate will expired');
                string body='Dear ';
                body=body+' '+m.emp_first_name1__c+' '+m.Emp_last_name2__c+' ,';
                body=body+'<br/>Your'+' ';           
                body=body+''+m.certification_Names_del__c+ 'Certificate will be Expired on...'+' '+m.Certification_Expired_Date__c+' ,';
                body=body+'Kindly Maintain your Certification.';
                body=body+'<br/>Thanks&Regards,';
                body=body+'<br/>Saleforce COE Team';
                mail.setHtmlBody(body);
                mailList.add(mail);         
            }

            Messaging.sendEmail(mailList);
        }
    }

    global void finish(Database.BatchableContext bc) {
        
    }
}

Test Class its showing 29% only

@isTest(SeeAllData = true)
public class CertificationEmailTestClass 
{
    @isTest
    public static  void CertificationEmailNotification(){
        
              
        list<Employees__c> ss=new list<Employees__c>();
        Employees__c em=new Employees__c();
        em.name='test89';
        em.Wissen_Employee_ID__c='1456';
        em.Last_Name__c='dumay288';
        em.Working_Email__c='[email protected]';
        em.Date_Of_Joining__c= Date.newInstance(1988, 2, 17);
        em.Laptop_W_P_CLIENT__c='Wissen';
        em.Country__c='India';
        ss.add(em);
        
        insert ss; 
        Employees__c Emp = [Select Last_Name__c,Name From Employees__c where name = 'test89'];
        
             
       // TestFactory.certification();
       list<Certification__c> css=new list<Certification__c>();
        
        Certification__c cer=new Certification__c(Employee__c = em.id);
        cer.Employee__c = emp.id;
        cer.Wissen_COE__c='salesforce coe';
        cer.certification_Names_del__c='Admin 201';
        cer.Certifications_Email__c='[email protected]';
        cer.Manager_Email_id__c = '[email protected]';
        cer.Certifications_Date__c = system.today().adddays(-30);   
        
        css.add(cer);
        insert css;
        
        
        Test.startTest();
        CertificationEmail Email = new CertificationEmail();
        DataBase.executeBatch(Email);
        
        
        Test.stopTest();
       
    }

    @isTest
    public static void CertificationEmailNotification2(){
        
        Test.startTest();
        scheduleExpireNotify sh1 = new scheduleExpireNotify();
        String sch = '0 0 2 * * ?'; 
        system.schedule('Test Territory Check', sch, sh1); 
        Test.stopTest();

    }
}

1 Answer 1

1

When you look at the batch code you see that almost all of the code in execute is within a !Test.isRunningTest condition. As such there's no surprise there is such low coverage.

To increase the coverage you could move the !Test.isRunningTest condition to only be around the actual Messaging.sendEmail call, such as:

global void execute(Database.BatchableContext bc, List<Certification__c> recs) {
    system.debug(recs.size());
    List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
    for(Certification__c m : recs) {
        List<String> toAddresses = new List<String>();
               
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        toAddresses.add(m.Certifications_Email__c);
        mail.setToAddresses(toAddresses);
        list<string> ccaddress = new list<string>();
        ccaddress.add(m.Manager_Email_id__c);
        mail.setCcAddresses(ccaddress);
        mail.setSubject('Certificate will expired');
        string body='Dear ';
        body=body+' '+m.emp_first_name1__c+' '+m.Emp_last_name2__c+' ,';
        body=body+'<br/>Your'+' ';           
        body=body+''+m.certification_Names_del__c+ 'Certificate will be Expired on...'+' '+m.Certification_Expired_Date__c+' ,';
        body=body+'Kindly Maintain your Certification.';
        body=body+'<br/>Thanks&Regards,';
        body=body+'<br/>Saleforce COE Team';
        mail.setHtmlBody(body);
        mailList.add(mail);         
    }

    if(!test.isrunningtest()){   
        Messaging.sendEmail(mailList);
    }
}

Some other points you might like to consider:

  1. Use of an email template for the message subject and body, with mailmerge expressions, to allow admins to change the wording without the need to modify code. See this previous Q&A for some info.
  2. Avoidance of custom email fields and the use of User IDs (setting the Target ID) to minimize use of daily email limits. See this previous Q&A for more.
  3. You don't need to use global access modifier unless your batch is designed to be shared across namespace boundaries (which is quite unusual unless you are an ISV). See the documentation for more.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .