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.
I would suggest the best approach would be to ensure the trigger doesn’t execute the batch if
Test.IsRunningTest()istrue, and then test the batch class with it’s own test method. I suspect your trigger is fired twice and so batch instances are created and run more than one.Using a dedicated test method you can execute the batch specifying a limit on the query, and you should use the optional batch size parameter to control the number of calls to execute, i.e. if your limit is 50, but you do this:
It’ll still need to call the
execute()method twice to cover all the records, and this is where you hit problems like the one you mentioned.