0

Calling batch class from batch class. I have a issue in test class coverage where for the first batch class coverage below code is working fine.

Test.startTest();
BatchClass1 batch1 = new BatchClass1(arg1,arg2);
Database.executeBatch(batch1,200);
Test.stopTest();

But for the second batch class coverage below code is not working.

 Test.startTest();
 BatchClass2 batch2 = new BatchClass2(arg1,arg2);
 Database.executeBatch(batch2,200);
 Test.stopTest();

I changed in above code and now it's working fine. Below is the working code - objList is query list from start method.

BatchClass2 batch2 = new BatchClass2(arg1,arg2);
Database.QueryLocator ql = batch2.start(null);
batch2.execute(null,objList);
batch2.Finish(null);

My question is - Why Database.BatchableContext BC is require NULL for successful test?

1
  • Can you check if there are any errors during execution of batch2 from case 2?
    – kurunve
    Jun 28, 2017 at 8:35

1 Answer 1

3

In your original case your test is calling the system class Database.executeBatch which in turn calls the start/execute/finish methods of your batchable and provides an instance of Database.BatchableContext in that process.

In the changed case you are directly calling the start/execute/finish methods of the batchable but because there is no way for you to create an instance of Database.BatchableContext you have to pass in a null (on the assumption that the batchable doesn't use that value or includes a null guard in its logic).

You will have to examine the batchable source code to figure out why the results are different. To test the batchable running in the same context as its actual use, best to get the Database.executeBatch case working.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .