+ Start a Discussion

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

I have set the size of batch to 1 in apex scheduler, i don't know how to set in test class. please help
My apex scheduler code:
global class ArticleBatchScheduler implements Schedulable{
   global void execute(SchedulableContext sc) {
      // Implement any logic to be scheduled. We now call the batch class to be scheduled. Parameters of ExecuteBatch(context,BatchSize)
      database.executebatch(New ArticleBatch(),1);

Test Class:
public class TestBatchApex{
    static testmethod void validateArticleBatchScheduler(){
            String CRON_EXP = '0 0 0 1 1 ? 2025';  
            String jobId = System.schedule('testScheduledApex', CRON_EXP, new ArticleBatchScheduler());
            CronTrigger ct = [select id, CronExpression, TimesTriggered, NextFireTime from CronTrigger where id = :jobId];
            System.assertEquals(CRON_EXP, ct.CronExpression); 
            System.assertEquals(0, ct.TimesTriggered);
            System.assertEquals('2025-01-01 00:00:00', String.valueOf(ct.NextFireTime));
James LoghryJames Loghry
Your first issue is that you are using "SeeAllData=true" and thus working with existing data, likely many records.

Next, you have set your batch size to 1, and are likely querying multiple records in the start method of your ArticleBatch class.  Because you're likely querying more than 1 record, your batch tries to kick off a second time, and throws the "No more than one batch" exception.

I suggest you remove your SeeAllData=true annotation and create your own data.  That way you can dictate how many records the batch class sees.  In addition to that, see if you can't increase your batch size that you're setting in your schedulable class.