You need to sign in to do that
Don't have an account?
Hussein 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:
And here is the test class:
These two functions are not covered:
1- sendAlertEmail(Opportunity quot),
2- getEmailTemplate()
I will appreciate any kind of help. Thank you
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()
I will appreciate any kind of help. Thank you
In your test class, you have declared And you are calling the sendAlertEmail function from the batch class
Shouldn't it be
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