+ Start a Discussion
JoshTonksJoshTonks 

Test Class For Scheduable Apex Batch

I have written a Batch Class which is called by a Scheduable class I have written. Im still trying to get my head round tests classes.

I have a test class for the Batch Class. But i dont know how to do write a test class where the schedulable class is calling a Batch Class. This is my Schedule class can someone explain to me what im actually testing for on this one because like I said i fully understand test classes.
 
global class OrderBatchEditScheduler implements Schedulable{
    global void execute(SchedulableContext sc)
    {
       
        OrderDailyBatchEdit b = new OrderDailyBatchEdit();
      
        Database.executebatch(b,200);
    }
   
}

 
Best Answer chosen by JoshTonks
MKRMKR
Hi,

I suggest that you do the following with Test.startTest and Test.stopTest. Usually there is no need to verify that the actual scheduling works, just that the execute method does what it should.
@isTest
private class OrderBatchEditSchedulerTest {

     @isTest
     static void testOrderBatchSchedule() {
          
          //Create test data needed to verify the batch job
          //...
          Test.startTest();
          OrderBatchEditScheduler scheduler = new OrderBatchDailyScheduler();
          scheduler.execute(null);
          Test.stopTest();


          //Query data to verify the results
          //...

     }

}

Regards,
MKR

All Answers

MKRMKR
Hi,

I suggest that you do the following with Test.startTest and Test.stopTest. Usually there is no need to verify that the actual scheduling works, just that the execute method does what it should.
@isTest
private class OrderBatchEditSchedulerTest {

     @isTest
     static void testOrderBatchSchedule() {
          
          //Create test data needed to verify the batch job
          //...
          Test.startTest();
          OrderBatchEditScheduler scheduler = new OrderBatchDailyScheduler();
          scheduler.execute(null);
          Test.stopTest();


          //Query data to verify the results
          //...

     }

}

Regards,
MKR
This was selected as the best answer
JoshTonksJoshTonks
Wow that was quick thank you MKR that makes sense now. You have helped me massively there thank you.