function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sri549Sri549 

Unable to Run Test Class for Batch

Hey ,

I am unable to write perfect test class for Batch apex. I am getting an exception like " No More than one ExecuteBatch ca be called from within test method.Please make sure the Iterable returned from your start method matches the batch size, resulting in oe executebatch invocation".

Could some one help me in this situation.

Thanks!
Srinivas
AnudeepAnudeep (Salesforce Developers) 
Hi,

Generally we see this error when processing more than 200 records in test class for Batch apex class

Resolution:
  • Try to pass the less than 200 records in 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 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)); }
Note
  • Enclose the executeBatch in Test.startTest() and Test.stopTest()
  • Always try to use @SeeAllData=false

Please see https://help.salesforce.com/articleView?id=000330685&language=en_US&type=1&mode=1 (https://help.salesforce.com/articleView?id=000330685&language=en_US&type=1&mode=1)

Anudeep