I have a batch job that does the following :

  • Makes Http callout
  • Create Cases
  • Calls the next Batch job (In the finish function) i.e.


public void finish(Database.BatchableContext context) {
    if (LOG_THIS_CLASS) System.debug('<Arcus> In the finish function of CaseCreation class.. ');

    AsyncApexJob batchJob = [
        SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email
        FROM AsyncApexJob
        WHERE Id = :context.getJobId()

    Integer jobs = [
        SELECT COUNT() 
        FROM AsyncApexJob 
        WHERE JobType = 'Batch Apex' 
        AND Status IN ('Queued', 'Processing', 'Preparing')
    Integer scopeSize = Limits.getLimitCallouts() - Limits.getCallouts();
    System.debug('<Arcus> The scope size == ' + scopeSize);
    if (jobs > 4) {
        // try again in a minute
        Datetime sysTime = System.now().addSeconds(60);
        String chronExpression = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();

        CaseScheduler scheduledBatch = new CaseScheduler();
        System.scheduleBatch(new AccountCaseLink(), 'Case Account Link' + sysTime, 1, scopeSize);                
    } else {
        AccountCaseLink batch = new AccountCaseLink();
        Database.executeBatch(batch, scopeSize);

The second batch job does another http callout based on the ids of the cases created and does account matching for each case. To test this, I have created a HttpCalloutMock and just set the mock in my test class. When tested I get error : System.NullPointerException: Attempt to de-reference a null object. Any help? Thanks

  • Thanks @Eric, I tried my code and it works well.. I have a schedulable class which calls the first batch class and the finish function just calls the next batch as shown in my code above
    – codeSlayer
    Jan 20, 2015 at 14:13
  • 1
    @Eric the linked code example is oudated and beyond fragile and ugly. It is possible to execute a batch from the finish() method of another batch without any problems. We are doing this for some time now and rely heavily on it. Jan 20, 2015 at 14:35
  • You are correct. starting with v26 you can now do this....
    – Eric
    Jan 21, 2015 at 2:58

2 Answers 2


When you have a batch finish() launch a second batch via executeBatch(), you must use two testmethods:

  1. Testmethod 1 - tests the first batch start-execute-finish sequence
  2. Testmethod 2 - tests the second batch start - execute - finish sequence as if it had been launched by the first batch's finish().

SFDC won't actually execute a batch in a testmethod until the Test.stoptest() is reached and you won't be able to assert the results of batch 2.

Testing batches with http callouts and setup of test data requires testing start(), execute(), and finish() explicitly if you get an Uncommitted Work pending error. See here and here.


After much searching and trying, I figured there is not set way of testing this other than checking if (Test.isRunningTest()) at the start of the finish function. If test is running, do nothing else execute the next batch as shown in my original code.

  • There is no reason to even check for test.isrunning test in the finish method. It will serve no point
    – Eric
    Jan 21, 2015 at 2:55
  • well it does serve a point which is to basically skip the rest of the finish method if Test.isRunning is true which will not really test the batch process but I had no other way of testing..
    – codeSlayer
    Jan 21, 2015 at 13:12
  • It will not run the batch anyway. so If all you are doing int he finish method is executing the batch then the check to see if test is running serves no point.
    – Eric
    Jan 22, 2015 at 15:50
  • The problem is if I dont put this condition in, the test fails. And because there is no set way of testing chained batch apex, i just put the testRunning? in the finish() in order to skip the scheduling of the next batch job..
    – codeSlayer
    Jan 23, 2015 at 14:58

You must log in to answer this question.

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