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
Neha KakrooNeha Kakroo 

Test class - schedulable class

Hi can somebody help me in writting a test class for the code below:

 

 

global class DailyChangeHistoryMailer implements Schedulable {
    global void execute(SchedulableContext SC) {
        Database.executeBatch(new ChangeHistoryMailer()); 
    }
}



Sandeep001Sandeep001

This is probably the Scheduler class for your batch apex class. Try the below code and let me know if it is useful:-

 

@isTest
private class TestScheduler{
    
    static testmethod void testCase(){
        
        DateTime currTime = DateTime.now();
        Integer min = currTime.minute();
        Integer hour = currTime.hour();
        String sch;
        
	if(min <= 58)
            sch = '0 '+ (min + 1) + ' ' + hour + ' * * ? '+ currTime.year();
        else          
            sch = '0 0 '+ (hour + 1) + ' * * ? '+ currTime.year();
        
        Test.startTest();
        
	DailyChangeHistoryMailer obj = new DailyChangeHistoryMailer();                
	String jobId = system.schedule('test', sch, obj);        
        CronTrigger ct = [SELECT id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger where id = :jobId];        
        System.assertEquals(sch, ct.CronExpression);                                      
        database.executeBatch(new ChangeHistoryMailer());        
        	
        Test.stopTest();
    }

}