+ Start a Discussion
RJ12RJ12 

Test class to cover Flow Interviews in Batch Apex

I wrote batch apex and it sends email using Flows. (I wrote all the email functionality in Flows and called that flow in my batch class)
global class BatchDocumentsUpdate implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        Date d = Date.today();
        String query = 'SELECT Id, Contact__c, Expiry_Date__c, Reminder_date__c, ContactStatus__c FROM Documents__c where Contact__c!=null AND Expiry_Date__c!=null AND ContactStatus__c=\'On Assignment\' AND Reminder_date__c=:d';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Documents__c> scope)
    {
        for(Documents__c a : scope)
        {
            Map<String, Object> params = new Map<String, Object>();
            params.put('CLId',a.Id);
            Flow.Interview.EmailReminder calcFlow = new Flow.Interview.EmailReminder(params);
			calcFlow.start();
            a.Reminder_date__c = Date.today()+1;          
        }
        update scope;
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}
'CLId' is a variable in the flow.
'Email Reminder' is the flow name.

I wrote a test class for this. But I'm unable to cover the EXECUTE method.
Documents__c is a child to Contact object.
@isTest
private class BatchDocumentsUpdate_Test {
    static testMethod void testMethod1(){
            Account acc = new Account();
            acc.Name ='Acc Name';
  			insert acc;
        
        	Contact con = new Contact();
       		con.AccountId = acc.id;
       		con.LastName = 'Con Name';
            con.Work_Status__c = 'On Assignment';
       		insert con;
        
        	Documents__c cl= new Documents__c();
            cl.Contact__c = con.id;
            cl.Expiry_Date__c = System.today() + 10;
            cl.ContactStatus__c = 'On Assignment';
            insert cl;
        
        Test.startTest();
            BatchDocumentsUpdate obj = new BatchDocumentsUpdate();
            DataBase.executeBatch(obj); 
        Test.stopTest();
    }
}
Can anyone help me in getting the Execute method covered?

Thanks

 
Raj VakatiRaj Vakati
First YOUR code it should be like below 
 
global class BatchDocumentsUpdate implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        Date d = Date.today();
        String query = 'SELECT Id, Contact__c, Expiry_Date__c, Reminder_date__c, ContactStatus__c FROM Documents__c where Contact__c!=null AND Expiry_Date__c!=null AND ContactStatus__c=\'On Assignment\' AND Reminder_date__c='+d+'';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Documents__c> scope)
    {
        for(Documents__c a : scope)
        {
            Map<String, Object> params = new Map<String, Object>();
            params.put('CLId',a.Id);
            Flow.Interview.EmailReminder calcFlow = new Flow.Interview.EmailReminder(params);
			calcFlow.start();
            a.Reminder_date__c = Date.today()+1;          
        }
        update scope;
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}

Test Class
@isTest
private class BatchDocumentsUpdate_Test {
    static testMethod void testMethod1(){
            Account acc = new Account();
            acc.Name ='Acc Name';
  			insert acc;
        
        	Contact con = new Contact();
       		con.AccountId = acc.id;
       		con.LastName = 'Con Name';
            con.Work_Status__c = 'On Assignment';
       		insert con;
        for(Integer i =0 ; i<10;i++){
        	Documents__c cl= new Documents__c();
            cl.Contact__c = con.id;
            cl.Expiry_Date__c = System.today() + 10;
            cl.ContactStatus__c = 'On Assignment';
			c1.Reminder_date__c= System.today();
            insert cl;
        }
        Test.startTest();
            BatchDocumentsUpdate obj = new BatchDocumentsUpdate();
            DataBase.executeBatch(obj); 
        Test.stopTest();
    }
}