How to fix below error message in test class
System.UnexpectedException: No more than one executeBatch can be called from within a test method. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation.
Please note already tried adding Limit 200 as suggested here https://help.salesforce.com/articleView?id=000330685&type=1&mode=1 but no success
My apex class is
global class ERTExtract255BatchClass implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(
'SELECT ID,Description,Case_Desc_255__c FROM Case'
);
}
global void execute(Database.BatchableContext bc, List<Case> scope){
// process each batch of record
List<Case> lstCase = new List<Case>();
for (Case cas : scope) {
string Strdesc = cas.Description ;
if(Strdesc.length()>255){
cas.Case_Desc_255__c = cas.Description.Left(255);
lstCase.add(cas);
}
}
update lstCase;
}
global void finish(Database.BatchableContext bc){
}
}
My test class is
@isTest(SeeAllData=false)
public class testERTExtract255BatchClass {
@IsTest
static void testBatchJob(){
List<Case> cases = new List<Case>();
for (integer i =0;i<300;i++)
{
Case c = new Case();
c.Description = 'aaaaaa'.rightPad(255,'b');
c.status = 'new';
c.Subject = 'test';
//add other mandatory fields
cases.add(c);
}
insert cases;
Test.startTest();
Database.executeBatch(new ERTExtract255BatchClass());
Test.stopTest();
Case Strcase = [Select id,Case_Desc_255__c from Case];
System.assertEquals(Strcase.Case_Desc_255__c.length(),255);
}
}
Any special reason why you need for (integer i =0;i<300;i++)
?
If you'd make 200 or less it should fit in nicely into "1 execute() call in unit test".
Or what you can do is to pass an optional parameter to Database.executeBatch
method. (in that documentation you can see the default is 200). So if you go Database.executeBatch(new ERTExtract255BatchClass(), cases.size());
it should be fine.