+ Start a Discussion
RJ12RJ12 

Test Class for Batch Apex that runs a Flow

I wrote a test class for batch apex but somehow getting 38% code coverage only. can anyone help me where I'm doing wrong?

I'm running a scheduler everyday morning to trigger Task, an Email alert from Flows when LMDate__c = today.
X1st_Business__c, LMDate  = formula field of type 'date '(Calculated based on last modified date)
X3rd_Business__c, X4th_Business__c are 'date' fields populated automatically using workflow when Lead status moved from 'Preparing' to 'Working'.

Batch Apex:
global class Batchable_Lead implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        Date d = Date.today();
        String query = 'SELECT Id, X1st_Business__c, X3rd_Business__c, X4th_Business__c, Status, LMDate__c FROM Lead where X1st_Business__c!=null AND X3rd_Business__c!=null AND X4th_Business__c!=null AND Status=\'Working\' AND LMDate__c=:d';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Lead> scope)
    {
        for(Lead l : scope)
        {
            Map<String, Object> params = new Map<String, Object>();
            params.put('LId',l.Id);
            Flow.Interview.Lead_Flows calcFlow = new Flow.Interview.Lead_Flows(params);
            calcFlow.start();       
        }
        update scope;
    }  
    global void finish(Database.BatchableContext BC)
    {
    }

}

Test Class:
 
@isTest
private class Batchable_Lead_Test {
static testMethod void testMethod1() 
    {
            Lead L= new Lead();
        	L.OwnerId = '0055A00000A6s7sQAB';
                L.LastName ='Lead Test Name';
                L.Company = 'Test Company';
                L.Status = 'Scheduling';
                L.LeadSource = 'Social';
        	L.Phone = '7364859473';
        	L.Bankruptcy__c = TRUE;
       	 	L.Street = '1234 abcd';
       	 	L.City = 'xxxxx';
       	 	L.State = 'yyyyy';
                L.PostalCode = '98765';
                L.Country = 'United States';
            insert L;
        
        Test.startTest();

            Batchable_Lead_Untouch obj = new Batchable_Lead_Untouch();
            DataBase.executeBatch(obj); 
            
        Test.stopTest();
    }
}

Unable to cover 'global void execute' class.
Jolly_BirdiJolly_Birdi
Hello @RJ12

Try this below test class:
 
@isTest
private class Batchable_Lead_Test {
static testMethod void testMethod1() 
    {
            Lead L= new Lead();
        	L.OwnerId = '0055A00000A6s7sQAB';
                L.LastName ='Lead Test Name';
                L.Company = 'Test Company';
                L.Status = 'Working';
                L.LeadSource = 'Social';
				L.Phone = '7364859473';
				L.Bankruptcy__c = TRUE;
				L.Street = '1234 abcd';
				L.City = 'xxxxx';
				L.State = 'yyyyy';
                L.PostalCode = '98765';
                L.Country = 'United States';
				L.X1st_Business__c = 'Test'; // Update its value if it is someId
				L.X3rd_Business__c = 'Test'; // Update its value if it is someId
				L.X4th_Business__c = 'Test'; // Update its value if it is someId
				L.LMDate__c = Date.today();
            insert L;
        
        Test.startTest();

            Batchable_Lead_Untouch obj = new Batchable_Lead_Untouch();
            DataBase.executeBatch(obj); 
            
        Test.stopTest();
    }
}



Please like and mark this as best answer if you find it positive.

Thanks,
Jolly Birdi