Loading

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

Data da publicação: May 3, 2026
Descrição

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

This error occurs because Salesforce limits test execution contexts to a single Database.executeBatch() call.

Resolução

This error occurs when a test class calls Database.executeBatch() more than once during a single test execution. Salesforce limits test contexts to a single executeBatch call. To resolve this, either limit the number of records passed to under 200, or use Test.isRunningTest() to conditionally skip chained batch job execution during test context.
The following options are available:

  • Limit records: Try to pass fewer than 200 records in the test class.
  • Use Test.isRunningTest(): Use this method to bypass the code that starts the second batch job in test context. To get coverage and assert behavior, test your second batch job in a separate test where you manually reproduce the database state that the second job expects.

The following example shows how to prevent a second batch job from being triggered during test execution by wrapping the Database.executeBatch call in a Test.isRunningTest() check:

public void finish(Database.BatchableContext)
{
    if(!Test.isRunningTest)
         Database.executeBatch(new MySecondBatchJob));
}
Notes:
  • Enclose executeBatch calls in Test.startTest() and Test.stopTest().
  • Always use @SeeAllData=false in your test classes.
Recursos adicionais

Test.isRunningTest — Apex Test Methods Reference

Número do artigo do Knowledge

000386014

 
Carregando
Salesforce Help | Article