+ Start a Discussion

Deployment error due to failure of test class



We are encountering the deployment error due to some test classes where batch apex class is called. The error occurring is:

"System.unexpectedException:No more than one executeBatch can be called within a test method."


In our test class, there are insert and update statements which in turn calls the batch apex from a trigger. We have also tried to limit the batch query by using "Test.isRunningTest()" method but we are again facing the same error.

The code works fine in sandbox and the error is coming only at the time of deployment to production.

Also, the test classes causing the error were working fine previously in the production.


Please provide some pointers/solution for the above mentioned error.


Thank you.



what you can do is , when you are querying for the batch, 


you can use this variable (test.isRunningTest), it is set to true when a test code is executed. You can't process more than 200 records in a test class for Batch, that's why the error message. 


Try something like this :


Suppose your batch query is on Account : Select Id, NAme From Account.


You should be handling this in the below way :



strQuery = 'Select Id, Name From Account Limit 200';


strQuery = 'Select Id, NAme From Account';


Here, it will return more than 200 records only when the code is not running through a test class. Let me know if this clears your doubt and helps you.


I followed your approach but still facing the same error for the test classes:

Failure Message: "System.UnexpectedException: No more than one executeBatch can be called from within a testmethod. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation.", Failure Stack Trace: "External entry point"


Can  u paste debug logs


Are you saying that within a single testmethod, you have both insert and update statements, and each of those is calling a batch apex?