function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
goutham.tatineni1.3893010493585044E12goutham.tatineni1.3893010493585044E12 

Help With Test class for Batch Apex on Opportunity Line Item

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.

@isTest(seeAllData=true)
private class TestConversionRate {

 @isTest static void MyUnitTest() {
 // Set up some local variables
String opportunityName = 'My Opportunity';
String standardPriceBookId = '';

PriceBook2 pb2Standard = [select Id from Pricebook2 where isStandard=true];
standardPriceBookId = pb2Standard.Id;

Account a = new Account(Name = 'Berlington');
  Insert a;
  
// set up opp and Verify that the results are as expected.
Opportunity o = new Opportunity(AccountId = a.id, Name=opportunityName, 
StageName='Discovery', CloseDate=Date.today());
insert o;
Opportunity opp = [SELECT Name FROM Opportunity WHERE Id = :o.Id];
//System.assertEquals(opportunityName, 'Berlington - 0 -');

// set up product2 and Verify that the results are as expected.
Product2 p2 = new Product2(Name='Test Product',isActive=true);
insert p2;
Product2 p2ex = [SELECT Name FROM Product2 WHERE Id = :p2.Id];
//System.assertEquals('Test Product', p2ex.Name);

// set up PricebookEntry and Verify that the results are as expected.
PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=p2.Id, UnitPrice=99, isActive=true);
insert pbe;
PricebookEntry pbeex = [SELECT Pricebook2Id FROM PricebookEntry WHERE Id = :pbe.Id];
//System.assertEquals(standardPriceBookId, pbeex.Pricebook2Id);

Win_Rate__c w = new Win_Rate__c(Name = 'Manpower - Brazil - 121-180',Win_rate__c = 90 );
  Insert W;

// set up OpportunityLineItem and Verify that the results are as expected.
OpportunityLineItem oli = new OpportunityLineItem(PriceBookEntryId=pbe.Id, OpportunityId=o.Id,WinRate__c = w.id,Hidden_Name__c = w.name, Quantity=1, TotalPrice=99);
insert oli;

OpportunityLineItem oliex = [SELECT PriceBookEntryId FROM OpportunityLineItem WHERE Id = :oli.Id];
System.assertEquals(pbe.Id, oliex.PriceBookEntryId); 
 
 
 opportunityLineItem Oppl = [Select Id,Name ,WinRate__c ,Hidden_Name__c from OpportunityLineItem where id =:oli.id];
 
 system.assertnotEquals(oppl.WinRate__c ,'');   
    
 }
 static testMethod void BatchProcessAccount_TestMethod (){
 Test.StartTest();        
   UpdateWinRateBatch objBatch = new UpdateWinRateBatch();
   ID batchprocessid = Database.executeBatch(objBatch);
   Test.StopTest();
 
 }

}


goutham.tatineni1.3893010493585044E12goutham.tatineni1.3893010493585044E12
This is the class

global class UpdateWinRateBatch implements Database.Batchable<sObject>{
    String query;
     
    global Database.querylocator start(Database.BatchableContext BC){
        Query = 'Select id,Age__c,Description from OpportunityLineItem where Age__c = 61 or Age__c =121 or Age__c =181 or Age__c = 366';   
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, List<OpportunityLineItem> scope){
       List<OpportunityLineItem> OpplList = new List<OpportunityLineItem>();
 
       for(OpportunityLineItem s : scope){
           s.Hidden_Name__c = 'OpportunityLineItem'; 
           OpplList.add(s); 
       }
 
       update OpplList;
    }
     
    global void finish(Database.BatchableContext BC){
    }
}


James LoghryJames Loghry
You're using @seeAllData in your test class.  This is likely causing several records to be picked up by the query in your batch class.  If the query returns more than 200 records, then it will fire off more than one batch (the maximum batch size is 200 records).  At most, a unit test can currently support one batch job (up to 200 records), so that is likely why you are seeing an exception.

Also, depending on your structure, if you are kicking off another batch job or in other words chaining batch jobs, then that would likely also result in an exception.  However, it doesn't look like this is the case.

Try removing the dependency on SeeAllData, and you should have better luck with your unit test.