5

This is the test method i have writtenfor batch apex ProductUpdate .Even if i have a single callout to Database.Executebatch() I am getting the following error. Please help !

@isTest (seeAllData=true)
global class testbatchProductUpdate 
{
    public static testMethod  void  pBatchUpdate()
    {

        /** Product to meet the Criteria in test class **/

        Product2 p = new Product2();
        p.Name = 'Test Product';
        p.Effective_Date__c = Date.Today();
        //p.CurrencyIsoCode= 'USD';
        p.Material_status__c='status';
        insert p;

        /** Product to meet the Criteria in test class **/


        test.startTest();
        batchProductUpdate bpu = new batchProductUpdate();
        database.Executebatch(bpu);
        test.stopTest();
    } 
}

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.", Failure Stack Trace: "External entry point"

0

6 Answers 6

6

With (seeAllData=true) all your existing Product2 data will be applicable to the scope of the batch, resulting in multiple batch executions. I suspect this is the problem.

It's highly advise to not use (seeAllData=true) actually, any specific reason why you turned it on ?

Update: based on your comment on an other answer: To find best practises and avoid running into limits, I usually start with reading the documentation.

Using Batch Apex

At 2/3th of the page it goes into some detail about testing, it actually starts right of with the governor limit you hit.

1
  • 8
    execute can only run once. Using SeeAllData=true makes it likely that execute will run multiple times.
    – sfdcfox
    Commented Aug 29, 2013 at 14:40
0

First thing to do in your code is remove the line (seeAllData=true). you are creating your own data in test so you don't need it.

Reason for this error is call of automatic execution of the same batch in same test context.

In test method either insert p; or batchProductUpdate bpu = new batchProductUpdate(); execute the batch before reaching to database.Executebatch(bpu);. So when this line executes your one batch process is already running at that point. In test class only one batch is allowed. It process one records list retrieved in first execution.

----Edit after comments---- A few things to consider in test class writing

  • When writing test class it is good to to avoid (seeAllData = true) as it makes your test class org dependent and causes unexpected issues as we don't know which type of data will be processed and in which numbers.
  • DML operation should be done with knowledge of triggers and classes associated with trigger on that object which will get fired.
  • In batch test methods it should consider that no database.Executebatch(batch); executed itself in any code line execution, if does than we don't need to write in test class.
6
  • how do you conclude 'insert p;' will launch the batch without having knowledge of triggers on Product2 ? Commented Aug 29, 2013 at 14:10
  • By trigger if you have written
    – Ashwani
    Commented Aug 29, 2013 at 14:11
  • thank you for the help. Removing (seeAllData=true) did the trick. But I am still not sure how two executeBatch call did happen. @responsive explained but i am not clear about two calls. insert p; is not calling any batch job nor there is any trigger. Even though commenting (seeAllData=true) worked I would be happy to know the reason on where am i going wrong or best practice to avoid such exceptions. Any suggestions ?
    – Soberano
    Commented Aug 29, 2013 at 14:32
  • Yea, it's not the insert that's causing the problem but the (seeAllData=true): with all the data in your org, when the batch code gets fired, there's more than enough records to create >1 batches to process hence the error (there can only be 1 batch to process when testing batch code). Commented Aug 29, 2013 at 14:41
  • @KeerthanTantry for to make focus on insert p; call because in most cases cyclic batch runs due to DML operation which executes the batch again and cause exception.
    – Ashwani
    Commented Aug 30, 2013 at 6:45
0

I also faced the same issue but I m not using @(SeeAllData=true) and also my batch is not calling second batch.But I noticed that I insert 4 rows for sample data but set the scope only 1 , example : Database.executeBatch(c,1) .So I insert only one sample data with scope setting to 1, the error is gone.Magic :)

0

Actually, Error due to multiple batches is running in the test class.

Let's assume, in ur org, there are 1000 records are there, you running batch class, by default, there is 200 batch size. So there is running 5 batches. right ?

So use 1000 batch size or more than that.

There is a limit of 2000 batch size at a time.

0

Error: "No more than one executeBatch can be called from within a test method"

Reaon: Your batch class dealing with more than 200 records. It means a single batch can process only 200 records, if there are more than 200 records exisits then your job will be executed in multiple batches of 200 records each. It is ok for dealing with actual processing. But in test classes, it will deal with only one batch class which is nothing but upto 200 records only.

Solution: Use Test.isRunningTest() condition in your main code/class to bypass this and test it for only upto 200 records which is nothing but one batch will be exuted this is what error suggesting. Then terminate the job to stop executing remaing records.

If(Test.isRunningTest()){
      ID jobID = Database.executeBatch(new BatchClassName, 200);
      System.abortJob(jobID);
}else{
      Database.executeBatch(new BatchClassName);
}

Happy coding

-3

Please go through this link it will help you.

http://boards.developerforce.com/t5/Apex-Code-Development/quot-No-more-than-one-executeBatch-can-be-called-from-within-a/td-p/242281

http://boards.developerforce.com/t5/Apex-Code-Development/Batch-Apex-Testing-Issues/td-p/198335

5
  • 2
    You would improve your answer by explaining why this helps and which parts of those pages are relevant. It is clear the question already shows understanding of testing batch apex. Commented Aug 29, 2013 at 14:11
  • The first link shows the most probable cause to the error, and it's your same conclusion, Sdry. Commented Aug 29, 2013 at 14:38
  • 4
    Long read to reach that, why have the OP go through that ? + the site linked to may go down/content removed. It is always better to at least provide a description, example, reasoning about the url you paste. Commented Aug 29, 2013 at 14:41
  • Nuh-unh, I saw it right away - 15 seconds. There's that handy "Go to solution" link. ;) Commented Aug 29, 2013 at 14:43
  • 1
    And I totally agree it's much much better to provide examples + reasoning to help those new to Apex and Salesforce -- it took me a long time of trolling through forums before I could wield Salesforce with any flare Commented Aug 29, 2013 at 14:44

You must log in to answer this question.

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