Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question
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? –  techtrekker Feb 14 '13 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 Feb 14 '13 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. –  techtrekker Feb 14 '13 at 10:39
    
Yeah I upgraded my test class to version26 but no luck. –  doga Feb 14 '13 at 10:44

1 Answer 1

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! ;-)

share|improve this answer
    
I tried suggested approach but same thing happened. –  doga Feb 15 '13 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. –  Andrew Fawcett Feb 15 '13 at 9:22
    
yep I did exactly same but it throws same error. –  doga Feb 15 '13 at 9:53
    
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'. –  Andrew Fawcett Feb 15 '13 at 12:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.