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
lawlaw 

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

 

Hello 

 

I receive the following error when running a test class:

EXCEPTION_THROWN|[EXTERNAL]|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.

 

Test Class is below. Any help wouldbe greatly appreciated.

 

public class TestScheduleBatchOnDSTADVISORUpdate {

//*****************************************************************************************
//*****************************************************************************************
/* Method # 1
Name : testScheduleBatchOnDSTADVISORUpdate
Function: Tests the functionality of the Apex Class
for the scheduling of the Batch Class
ScheduleBatchOnDSTADVISORUpdate

*/
public static TestMethod void testScheduleBatchOnDSTADVISORUpdate()

{
DateTime currTime = DateTime.now();
Integer min = currTime.minute();
Integer hour = currTime.hour();
String sch;


//Scheduling the class
if(min <= 58)
sch = '0 '+ (min + 1) + ' ' + hour + ' * * ? '+ currTime.year();
else
sch = '0 0 '+ (hour + 1) + ' * * ? '+ currTime.year();

Boolean runTestMethod;
//***************Test Starts****************
Test.startTest();

CAD_DST_Advisors__c dst=new CAD_DST_Advisors__c();
dst.name='Test_DST_ScheduleBatchOnDSTADVISORUpdate';
dst.CRD__c='5521521';
dst.REP_LAST_NAME__c='Testing';
dst.REP_WORK_CITY__c='Test_City';
dst.CRDChanged__c=true;
dst.UIDDirectlyFromDST__c=true;
insert dst;

// Creating the instance of the Apex Class to be tested "ScheduleBatchOnDSTADVISORUpdate"

ScheduleBatchOnDSTADVISORUpdate obj = new ScheduleBatchOnDSTADVISORUpdate ();
String jobId = system.schedule('test', sch, obj);

//CronTrigger : Represents a scheduled Apex job
CronTrigger ct = [SELECT id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger where id = :jobId limit 199];


System.assertEquals(sch, ct.CronExpression);
database.executeBatch(new BatchOnDSTUpdateADVISORCONTROLLER('select id,CRD__c, CRD_P__C,UID__C,REP_LAST_NAME__c,Email_p__c, LastName_City_State_Combo__c, REP_PRIMARY_CONTACT_ID__c ,REP_WORK_CITY__c ,REP_WORK_STATE__c, REP_EMAIL_ADDRS__c,Is_UID_Populated__c,uiddirectlyfromDST__c from CAD_DST_Advisors__c where CRDChanged__c=true and (Is_UID_Populated__c=true or UIDDirectlyFromDST__c=true)limit 199'));

System.abortJob(JobID);
//Assert Statements
System.assertequals('5521521',dst.CRD__c,'CRD__c didnot work');
System.assertequals('Test_DST_ScheduleBatchOnDSTADVISORUpdate',dst.name,'Name didnot work');
System.assertequals('Testing',dst.REP_LAST_NAME__c,'REP_LAST_NAME__c didnot work');
System.assertequals(true,dst.CRDChanged__c,'CRDChanged__c didnot work');
System.assertequals(true,dst.UIDDirectlyFromDST__c,'UIDDirectlyFromDST__c didnot work');

Test.stopTest();
}



//***************Test Ends****************


}

 

ForceMantis (Amit Jain)ForceMantis (Amit Jain)
Can you try running query used inside your execute batch directly in your test class and see how many records it return.

Default Batch size for a Batch Class is 200 and if your query return more than this it will run two batches which is not allowed in test class. I see that you have Limit 199 but still just for troubleshooting we have see where is error.

database.executeBatch(new BatchOnDSTUpdateADVISORCONTROLLER('select id,CRD__c, CRD_P__C,UID__C,REP_LAST_NAME__c,Email_p__c, LastName_City_State_Combo__c, REP_PRIMARY_CONTACT_ID__c ,REP_WORK_CITY__c ,REP_WORK_STATE__c, REP_EMAIL_ADDRS__c,Is_UID_Populated__c,uiddirectlyfromDST__c from CAD_DST_Advisors__c where CRDChanged__c=true and (Is_UID_Populated__c=true or UIDDirectlyFromDST__c=true)limit 199'));
Ramakrishna Madha 6Ramakrishna Madha 6
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