4

I am getting following error

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.

This test class was written earlier and working fine so far until spring13. Is there any change in spring13 which made to fail this test? or I have to rewrite entire test method? Any thoughts?

The batch I am calling in this test method in turn calls another batch from finish method. Could this be problematic?

4
  • 1
    Not sure about Spring'13, but is the executeBatch enclosed within a Test.startTest() and Test.stopTest(). Are there multiple executeBatch invocations within the same context? Commented Feb 14, 2013 at 10:23
  • Yes I have done that. Actually the batch I am calling in this test method in turn calls another batch from finish method. Could this be problematic?
    – doga
    Commented Feb 14, 2013 at 10:36
  • So I think Winter'13 introduced 'daisy-chaining' of batches andrewfawcett.wordpress.com/2012/10/14/…. Wonder if it could be the API version of your test / batch classes - worth updating to the latest API Versions. Commented Feb 14, 2013 at 10:39
  • Yeah I upgraded my test class to version26 but no luck.
    – doga
    Commented Feb 14, 2013 at 10:44

1 Answer 1

12

Behaviour: I can confirm despite the great new Spring'13 feature to daisy chain Batch jobs this restriction still applies in a Test context. While the documentation is good for describing best practices around testing Batch Apex. This limitation is a little buried in a code sample show in the topic, but does indeed read as follows.

// Create 200 test accounts - this simulates one execute.
// Important - the Salesforce.com test framework only allows you to test one execute.

Suggested Approach to Testing: What you have to consider is using the 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 behaviour. In doing so you will obvsiouly 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));
}

Hope this helps!

P.S. thanks for the blog reference TechTrekker! ;-)

4
  • I tried suggested approach but same thing happened.
    – doga
    Commented Feb 15, 2013 at 4:36
  • Hmmm that is odd, I've updated my answer with a small sample just to check we are on the same page with what I was suggesting. Commented Feb 15, 2013 at 9:22
  • yep I did exactly same but it throws same error.
    – doga
    Commented Feb 15, 2013 at 9:53
  • 1
    In that case unless there is some other code path that starts the job, maybe the issue is simply that your test code is passing in more than 200 rows? As the message states 'Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation.'. Where it says 'iterable' read also 'SOQL query'. Commented Feb 15, 2013 at 12:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .