Print this page

Error "System.UnexpectedException: No more than one executeBatch can be called from within a testmethod"

Knowledge Article Number 000176562
Description Issue : System.UnexpectedException: No more than one executeBatch can be called from within a testmethod.

Causes: Might be we are processing more than 200 records in test class for the batch apex.
Resolution Workarounds : 
=============

>> Try to pass the less than 200 records in test class.

>> What you have to consider is using the Test.isRunningTest to bypass the code starting the second job in this context. Meaning that you will have to test your second batch job in a separate test to get coverage and assert behaviour. In doing so you will obviously have to reproduce manually in the test code the state in the database the second job expects. Not ideal, but should work.

 
The following is an example of the change to avoid the second batch job being executed during test execution.
 
 
public void finish(Database.BatchableContext)
{
    if(!Test.isRunningTest)
         Database.executeBatch(new MySecondBatchJob));
}


>> Enclose the executeBatch in Test.startTest() and Test.stopTest()

>> always try to use @SeeAllData=false




promote demote