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
Internal PartnerInternal Partner 

BatchSize must not be null: Error by testing a Batch Scheduler test class

Hi all,

I have the following Scheduler apex class:
 
public class Example_Scheduler implements Schedulable {
    
  public void execute(SchedulableContext sc){
        Example_Batch batch1 = new Example_Batch();
    Database.executebatch(batch1,batch1.lim);
    }
}
and the following corresponding test class:
 
@isTest
public class Example_Scheduler_Test {
    
   static testMethod void testMethod1() {
        insert new CS_Example__c(Batch_limit__c = 100, Name = 'Test');
        Test.StartTest();
            Example_Scheduler sh1 = new Example_Scheduler();      
            String sch = '0 0 20 * * ?';
            system.schedule('Test check', sch, sh1);
        Test.stopTest();
    }
}

Where CS_Example__c is a custom setting with the following custom field:

Batch_Limit__c --> Number (18,0)

When testing the test calls I get the following error:
 
System.InvalidParameterValueException: batchSize must not be null.
StackTrace: Class.Example_Scheduler.execute: line 5, column 1

I could not correct this so far. Can someone advise me what exactly is not letting run the test successfully so I can correct it?.

 
Raj VakatiRaj Vakati
Change your code as below
 
public class Example_Scheduler implements Schedulable {
   

public Integer count = 200 ; 

public Example_Scheduler(Integer count)
{
this.count = count ;
} 
  public void execute(SchedulableContext sc){
        Example_Batch batch1 = new Example_Batch();
    Database.executebatch(batch1,count );
    }
}


Test class
 
@isTest
public class Example_Scheduler_Test {
    
   static testMethod void testMethod1() {
        insert new CS_Example__c(Batch_limit__c = 100, Name = 'Test');
        Test.StartTest();
            Example_Scheduler sh1 = new Example_Scheduler(200);      
            String sch = '0 0 20 * * ?';
            system.schedule('Test check', sch, sh1);
        Test.stopTest();
    }
}


 
Internal PartnerInternal Partner
Thank you very much :), but I wouldn't like to change anything in the Schdeuler class. I want to know what is wrong in my test class, which is failing by testing it.
Raj VakatiRaj Vakati
The below bold code is wrong .. Check the CS_Example__c  setting value for the for batch size  

And in test class you need to insert CS_Example__c  custom settings objects 

 
public class Example_Scheduler implements Schedulable {
    
  public void execute(SchedulableContext sc){
        Example_Batch batch1 = new Example_Batch();
    Database.executebatch(batch1,batch1.lim);
    }
}

@isTest
public class Example_Scheduler_Test {
    
   static testMethod void testMethod1() {
        Test.StartTest();
 insert new CS_Example__c(Batch_limit__c = 100, Name = 'Test'); 

            Example_Scheduler sh1 = new Example_Scheduler();      
            String sch = '0 0 20 * * ?';
            system.schedule('Test check', sch, sh1);
        Test.stopTest();
    }
}




 
Internal PartnerInternal Partner
The value is 100. I already checked this. So, I don't understand what is incorrect.
Raj VakatiRaj Vakati
give me batch class ?