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
Sandeep TechAffinitySandeep TechAffinity 

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.

Hi Guys,

I have a Batch Class and the Test class. Test coverage is 100% but deployment got validation error.

 

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


The code is

global class addTaskObjectToQueues implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext bc){
        Set<String> queueSet = new Set<String>();
        List<QueueSobject> lstQueueSobject = [SELECT QueueId FROM QueueSobject where SobjectType = 'Task'];
        for(QueueSobject qs : lstQueueSobject){
            queueSet.add(qs.QueueId);
        }
        string query = 'SELECT Id FROM Group where Type = \'queue\' AND Id NOT IN : queueSet'+(Test.isRunningTest()?' LIMIT 200':'');
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc, List<Group> scope){
        List<QueueSobject> supportedObjList = new List<QueueSobject>();
       for(Group gp : scope) {
            QueueSobject qs = new QueueSobject();
            qs.QueueId = gp.id;
            qs.SobjectType = 'Task';
            supportedObjList.add(qs);
        }
        if(!supportedObjList.isEmpty()) {
            insert supportedObjList;
        }
        
    }
    global void finish(Database.BatchableContext bc){
        
    }
    
}
Test Class
=========
@isTest 
public class addTaskObjectToQueuesTest {
    
    static testMethod void testMethod1() 
    {
        test.startTest();
        List<Group> lstGroup = new List<Group>();
        for(Integer i=0 ;i <50;i++)
        {
            Group grp = new Group();
            grp.name = 'test'+i;
            grp.type = 'Queue';
            lstGroup.add(grp);
        }
        
        insert lstGroup;
        
        List<QueuesObject> qsobj = new List<QueuesObject>();
        integer counter = 0;
        for(Group g : lstGroup){
            QueuesObject q = new QueueSObject();
            q.QueueID = g.Id;
            q.SobjectType = 'Lead';
            
        }
        insert qsobj;
        
        
        DataBase.executeBatch(new addTaskObjectToQueues(),150);
        test.stopTest();
    }
    
}

​​​​​​​
 
PriyaPriya (Salesforce Developers) 
Hey Sandeep,

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);
}

Also refer this link :- 
https://help.salesforce.com/s/articleView?language=en_US&type=1&id=000330685


Kindly mark it as best answer so that it can help other as well.

Thanks