System.UnexpectedException: No more than one executeBatch can be called from within a testmethod

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”

Answer

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.

Attribution
Source : Link , Question Author : Soberano , Answer Author : Samuel De Rycke

Leave a Comment