Loading

Salesforce Apex Error: 'No more than one executeBatch can be called from within a testmethod'

Publiseringsdato: Sep 11, 2025
Beskrivelse

When processing more than 200 records in a test class for the batch apex class, you may see an error: System.UnexpectedException: No more than one executeBatch can be called from within a testMethod.

Løsning

In order to resolve the error, review the following:

 

  • Try to pass the less than 200 records in a test class.
  • Use 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 behavior. In doing so, you will have to reproduce manually in the test code, the state in the database that the second job expects. 

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));
}
Note
  • Enclose the executeBatch in Test.startTest() and Test.stopTest()
  • Always try to use @SeeAllData=false
Test.isRunningTest
Knowledge-artikkelnummer

000386014

 
Laster
Salesforce Help | Article