0

here is the Apex class:

global class GroupBatchSchedule implements Schedulable {

    global void execute(SchedulableContext sc) {

        GroupBatch b = new GroupBatch(); 

        if(!Test.isRunningTest())

        database.executebatch(b);
    }

}

Here is its test class:

@isTest
Public class GroupBatchScheduleTest{

    public static testMethod void testschedule() {

        Test.StartTest();

        GroupBatchSchedule testsche = new GroupBatchSchedule();

        String sch = '0 0 23 * * ?';

        system.schedule('Test', sch, testsche );

        Test.stopTest();
    }

}

NOTE: if I do not use if(!Test.isRunningTest()) in the apex class the its code coverage is 100% but failing with the 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.

1 Answer 1

0

This can be because your Start query may be on Setup object like user. So you have to limit the query to 200 or batch size in your main class like below.

global Database.QueryLocator start(Database.BatchableContext BC){
    String query = 'SELECT fields from object '+(Test.isRunningTest()?' LIMIT 200':'');
    return Database.getQueryLocator(query);
}

This type of error is because as the will apply to test data that exceeds the scope size. So if you have more than 200 records in your test data set, you will hit this. As this is setup object so the query will result all the records which satisfies the condition of Start() method

2
  • But I'm not quering anything. You can see the above code.
    – user122180
    Sep 13, 2022 at 7:47
  • You may be quering in the batch class called GroupBatch in Start method. You have to add the filter there Sep 13, 2022 at 7:52

You must log in to answer this question.