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
Hussein Azeez 2Hussein Azeez 2 

Hi, everyone I'm new here and currently, working on Batch class I'm trying to get the required coverage or more but I couldn't manage that.

here is the class:
 
global class Batch_EmailCheckLastModified implements Database.Batchable<sObject>{
    private String query;
    global Batch_EmailCheckLastModified(){
        System.debug('enter batch'); 
        this.query = 'SELECT Id,CreatedDate,LastModifiedDate,Expire_date__c,owner.name,owner.email,Quotation_Number__c, Inactive__c,PO_Approve_by_manager__c FROM Opportunity';
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        System.debug('starting'); 
        return Database.getQueryLocator(this.query);
    }
    
    global void execute(Database.BatchableContext BC, List<sObject> scope){
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        for(SObject s:scope){
            Opportunity opp=(Opportunity)s;
            Date created = date.newInstance(opp.CreatedDate.year(),opp.CreatedDate.month(),opp.CreatedDate.day());
            Date LastModify = date.newInstance(opp.LastModifiedDate.year(),opp.LastModifiedDate.month(),opp.LastModifiedDate.day());
            Date todayDate = date.today();
            Integer dayBetweenCreated=created.daysbetween(todayDate);
            if((dayBetweenCreated>3)&&(created.isSameDay(LastModify))){
                 mails.add(sendAlertEmail(opp)); 
            }
                
        }
        send(mails);
        
    }
    
    global void finish(Database.BatchableContext BC){
        
    }
   private Messaging.Singleemailmessage sendAlertEmail(Opportunity quot) {
        EmailTemplate emailTemplate = getEmailTemplate();
        String[] userEmails = new List<String>();
        String Body = emailTemplate.Body;
        String fullFileURL = URL.getSalesforceBaseUrl().toExternalForm() +
            '/' + quot.id; 
      
        Body = Body.replace('Dear User,', 'Dear User,<br/>');
        Body = Body.replace('Thank you,', '<br/><br/>Thank you,<br/>');
        Body = Body.replace('{!Opportunity.Link}', '<br/>'+fullFileURL);
        Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();
        
        userEmails.add('warunya@crm-c.club');
        email.setHtmlBody(Body);
        email.setToAddresses(userEmails);
        email.setSubject(emailTemplate.Subject);
        
        //Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
        return email;
    }
    
    private void send(List<Messaging.SingleEmailMessage> mails){
        Messaging.sendEmail(mails);
    }
    
    private EmailTemplate getEmailTemplate() {
        EmailTemplate emailTemplate = [
            SELECT 
                Id, 
                HtmlValue,
                Subject,
                Body
            FROM EmailTemplate 
            Where Name = 'Update Opportunity Notification'
        ];
        return emailTemplate;
    }

}

And here is the test class:
 
@isTest (SeeAllData = true)
public class Test_Batch{
    static testMethod void Test_Batch_EmailCheckLastModifided_Method(){
        Test.startTest();
        
        Batch_EmailCheckLastModified batch = new Batch_EmailCheckLastModified();
        DataBase.executeBatch(batch);
        
        BatchOppProcuReserveExpiry batch2 = new BatchOppProcuReserveExpiry();
        Opportunity inputOpp=[select id,owner.name,Quotation_Number__c,owner.email from Opportunity limit 1];
        
        EmailTemplate newEmailTemplate = new EmailTemplate();
        newEmailTemplate.Name='Expired Opp';
        newEmailTemplate.DeveloperName='TestName';
        newEmailTemplate.TemplateType = 'Text';
		newEmailTemplate.FolderId = UserInfo.getUserId();
        newEmailTemplate.Body='{!Opportunity.OwnerFirstName} test {!Opportunity.Quotation_Number__c} test {!Opportunity.Link}';
        insert newEmailTemplate;
        batch2.sendAlertEmail(inputOpp);
        DataBase.executeBatch(batch2);

        Datetime dt = Datetime.now().addMinutes(1);
        String CRON_EXP = '0  00 1 3 * ?';
        
        System.schedule('Sample_Heading',CRON_EXP,new Schedule_EmailCheckLastModified()); 
        
        
        Test.stopTest();
        
    }
}

These two functions are not covered:
1- sendAlertEmail(Opportunity quot),
2- getEmailTemplate()

User-added image

I will appreciate any kind of help. Thank you
Best Answer chosen by Hussein Azeez 2
RD@SFRD@SF
Hi Hussein,

In your test class, you have declared 
BatchOppProcuReserveExpiry batch2 = new BatchOppProcuReserveExpiry();
And you are calling the sendAlertEmail function from the batch class
 
batch2.sendAlertEmail(inputOpp);

Shouldn't it be
 
Batch_EmailCheckLastModified batch2 = new Batch_EmailCheckLastModified();
and call the sendAlertEmail function from this class.

If you call sendAlertEmail function from the function getEmailTemplate would also get covered.

Hope it helps
RD​