I need help writing test cases for an email batch job. In the batch job, the users associated with the Franchisee__c field (a Lookup to User) from the custom object Store_App_Proc__c are selected and emails are sent to each user. The problem is that I am not sure how to structure my test class or what to assert for after running the batch.
The following is the code of the email batch job:
global class Store_App_Proc_EmailBatchJob implements Database.Batchable<sObject>, Database.Stateful{
global Store_App_Proc__c sap;
global Id templateId;
global Store_App_Proc_EmailBatchJob(Store_App_Proc__c insap){
sap = insap;
templateId = [SELECT id, name FROM EmailTemplate WHERE developername = 'Email_to_FR'].id; }
global Database.QueryLocator start(Database.BatchableContext BC){
Store_App_Proc__c fr = [Select Franchisee__c From Store_App_Proc__c WHERE id =: sap.id limit 1][0];
String query = 'SELECT Id FROM User WHERE Id= \'' + String.escapeSingleQuotes(fr.Franchisee__c) + '\'';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<User> scope){
for(User user : scope){ // send email per user Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); email.setTemplateId(templateId); email.setReplyTo('example@example.com'); email.setSenderDisplayName('AGI Development'); email.setTargetObjectId(user.Id); email.setWhatId(sap.Id); email.setSaveAsActivity(false); Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email}); } }
global void finish(Database.BatchableContext BC){} }
This is what I have so far for the test case:
@isTest
private class Store_App_Proc_EmailBatchJob_Test {
public static testMethod void emailBatchJobTest(){
Id templateId = [SELECT id, name FROM EmailTemplate WHERE developername = 'Email_to_FR'].id;
Account acct = new Account();
acct.Name = 'test1';
acct.AGI_Territory__c= 'Europe';
acct.Sub_Territory__c = 'Western Europe';
acct.AGI_Master_Country__c= 'Portugal';
acct.AGI_Partner_Account__c = 'ZT Two Image';
insert acct;
// setting all the fields required for Store_App_Proc__c object
Store_App_Proc__c storeAppProc = new Store_App_Proc__c(AGI_Account__c = acct.id);
storeAppProc.AGI_Customer_Name__c = 'ALDO/ZT Two Image/Portugal';
//...
RecordType rt1 = [Select ID, Name ,DeveloperName From RecordType Where sObjectType = 'Store_App_Proc__c' and DeveloperName ='AGI_SAP_InitialStep' limit 1][0];
storeAppProc.RecordTypeId = rt1.id;
insert storeAppProc;
test.startTest();
//initiating an instance of the batch job
Store_App_Proc_EmailBatchJob b = new Store_App_Proc_EmailBatchJob(storeAppProc);
test.stopTest();
// what to assert?
}