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
chaithaly gowdachaithaly gowda 

Why more than once the execute method of batch class is not being called in apex test class

Hi

I have a test class for batch class that executes upon the insert of task and event records.

@TestSetup 
    static void CreateData()
    {
        Test.startTest();
     DataFactory.createTasks(3, 'Task Score', 'Not  Started','Operations - Task', true); 
     DataFactory.createEvents(3,'Event ActivityBactchScore','Operations - Event', True);
        Test.stopTest();
    }

static testMethod void TaskBatchTest() {
     //   insert TaskRecords;
     System.debug([Select id,totaljobitems,apexclassid,jobitemsprocessed FROM AsyncApexJob WHERE JobType='BatchApex' and status='completed']);

integer count=[Select count() FROM AsyncApexJob WHERE JobType='BatchApex' and status='completed'];

            system.assertEquals(1, count);
}

So ideally, two times the batch class needs to be executed upon isertion of task and event record.

But if I query asyncapexjob it is returning only one id, and when i checked debug log, the batch class execute method is called only once for task but not for events.

Can someone please let me know why this is happening and also how do i check for event batch execution .

Thanks
ShirishaShirisha (Salesforce Developers) 
Hi Chaithaly,

Greetings!

Please be informed that each test method is allowed to call this method only once.I would suggest you to take a look at the sample code with an example provided in the below blog.

https://www.sfdcamplified.com/2019/04/test-class-for-batch-apex-in-salesforce.html

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri