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
Jon Hayes CHIJon Hayes CHI 

Need help with batch apex test class code coverage

Hello,

I'm trying to navigate my way through this apex test case and I've not been successful. Could anyone assist with bringing coverage up from 46%?
Thank you.

Test Class:
@isTest
private class CHIEventsMonitorTest {
    @testSetup 
    static void setup() {
        List<Event__c> events = new List<Event__c>();
        // insert 10 events
        for (Integer i=0;i<10;i++) {
            events.add(new Event__c(name='Event '+i, 
                CreatedDate = DateTime.now(), LastModifiedDate = DateTime.now(), Event_End_Date__c = Date.today(), CHI_Publish_To_Website__c = false, CHI_Status__c = 'Planned', Send_to_Consultants_for_Assignment_c__c = false, EvaluationPilot__c = false, Send_Attendance_Email__c = false, Completed__c = false));
        }
        insert events;
    }
    static testmethod void test() {        
        Test.startTest();
        CHIEventsMonitor uca = new CHIEventsMonitor();
        Id batchId = Database.executeBatch(uca);
        Test.stopTest();
    }
    
}
Note - Most of this was accomplished with the help of Google (obviously, or I probably wouldn't be here)

Batch Class:
global class CHIEventsMonitor implements Database.Batchable<sObject>{
    List<Event__c> eventToUpdate = new List<Event__c>();
    Date dt = date.today();
    String query = 'Select Id, CHI_Status__c, Completed__c, Send_Attendance_Email__c From Event__c WHERE IsFinished__c = FALSE AND Event_End_Date__c =: dt';  

    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Event__c> event){
        for(Event__c c : event){
            if(c.Completed__c==FALSE){
                c.Send_Attendance_Email__c = TRUE;
                c.Completed__c = TRUE;
                eventToUpdate .add(c );         
            }

        }
        update eventToUpdate ;
    }

    global void finish(Database.BatchableContext BC){     
    }
}

 
Raj VakatiRaj Vakati
try this
 
@isTest
private class CHIEventsMonitorTest {
    @testSetup 
    static void setup() {
		    Date dt = date.today();

        List<Event__c> events = new List<Event__c>();
        // insert 10 events
        for (Integer i=0;i<10;i++) {
            events.add(new Event__c(name='Event '+i, 
                CreatedDate = DateTime.now(), LastModifiedDate = DateTime.now(),
				Event_End_Date__c = Date.today(),
				CHI_Publish_To_Website__c = false, 
				CHI_Status__c = 'Planned', 
				Send_to_Consultants_for_Assignment_c__c = false, 
				EvaluationPilot__c = false, 
				Send_Attendance_Email__c = false,
				IsFinished__c = false , 
				Event_End_Date__c  =dt ,
				Completed__c = false));
        }
        insert events;
    }
    static testmethod void test() {        
        Test.startTest();
        CHIEventsMonitor uca = new CHIEventsMonitor();
        Id batchId = Database.executeBatch(uca);
        Test.stopTest();
    }
    
}

 
Jon Hayes CHIJon Hayes CHI
Thank you for the fast response Raj. The code coverage is still 46%
Raj VakatiRaj Vakati
Got it .. 

You batch apex query is looks not correct  and give me whihc lines are not covering 

Change your batch apex class as below
 
global class CHIEventsMonitor implements Database.Batchable<sObject>{
    List<Event__c> eventToUpdate = new List<Event__c>();
    Date dt = date.today();
    String query = 'Select Id, CHI_Status__c, Completed__c, Send_Attendance_Email__c From Event__c WHERE IsFinished__c = FALSE AND Event_End_Date__c =\''+dt+'\'';  

    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Event__c> event){
        for(Event__c c : event){
            if(c.Completed__c==FALSE){
                c.Send_Attendance_Email__c = TRUE;
                c.Completed__c = TRUE;
                eventToUpdate .add(c );         
            }

        }
        update eventToUpdate ;
    }

    global void finish(Database.BatchableContext BC){     
    }
}


 
Jon Hayes CHIJon Hayes CHI
Lines 10 - 19 are not covered
Raj VakatiRaj Vakati
Can u update the apex class as shown above and try to run the test class .. 

You batch class is not correct i gueess 
 
global class CHIEventsMonitor implements Database.Batchable<sObject>{
    List<Event__c> eventToUpdate = new List<Event__c>();
    Date dt = date.today();
    String query = 'Select Id, CHI_Status__c, Completed__c, Send_Attendance_Email__c From Event__c WHERE IsFinished__c = FALSE AND Event_End_Date__c =\''+dt+'\'';  

    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Event__c> event){
        for(Event__c c : event){
            if(c.Completed__c==FALSE){
                c.Send_Attendance_Email__c = TRUE;
                c.Completed__c = TRUE;
                eventToUpdate .add(c );         
            }

        }
        update eventToUpdate ;
    }

    global void finish(Database.BatchableContext BC){     
    }
}

 
Jon Hayes CHIJon Hayes CHI
The query works as is - I have successfully run the batch on a schedule for the last few days. Updating with your change fails. I appreciate your help though Raj.

It appears my issue is the test class is not covering the updates to the records. 
Raj VakatiRaj Vakati
can u share the data model screenshot of the event object .. try ing to do in my org .. 

Is there any formulas 
Raj VakatiRaj Vakati
WIth the below test class i got 100 % .. can u check is running successfully or now 

 
@isTest
private class CHIEventsMonitorTest {
    @testSetup 
    static void setup() {
        Date dt = date.today();
        
        List<Event__c> events = new List<Event__c>();
        // insert 10 events
        for (Integer i=0;i<10;i++) {
            events.add(new Event__c(name='Event '+i, 
                                    CHI_Status__c = 'Planned', 
                                    Send_Attendance_Email__c = false,
                                    IsFinished__c = false , 
                                    Event_End_Date__c  =dt ,
                                    Completed__c = false));
        }
        insert events;
    }
    static testmethod void test() {        
        Test.startTest();
        CHIEventsMonitor uca = new CHIEventsMonitor();
        Id batchId = Database.executeBatch(uca);
        Test.stopTest();
    }
    
}



User-added image
Jon Hayes CHIJon Hayes CHI
Raj - this object is fairly large. I am beginning to wonder if the issue is I'm not putting enough data into the test records - maybe they're failing to create at all? Does that sound like a possibility? I am going to try adding more fields into the list and will report back.

Thank you for your help so far. 
Jon Hayes CHIJon Hayes CHI
Raj - I was able to get it to work by adding 
List<Event__c> existingListCompleted = [select id,Completed__c from Event__c where Completed__c = TRUE];
        System.assertequals(existingListCompleted.size(),0);

between the batch execution and test.stoptest

Thank you for your help.