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
Sanjat Samal 8Sanjat Samal 8 

How to write a test class for schedule apex class? For Below Code.

global class ClearHistoricalCaseScheduler implements Schedulable
{
  
    global void execute(SchedulableContext SC) 
    {    
        System.debug('ClearHistoricalCaseScheduler Start...');        
        
        ClearHistoricalCaseBatch batchDelete = new ClearHistoricalCaseBatch();
        //Get Clear Date string - 14 months ago
        
        Integer caseDuration =  0 - Integer.valueOf(GenericProjectConfig__c.getValues('MDM_CaseDuration').value__c);
        String clearDateString = DateTime.Now().addMonths(caseDuration).formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
        batchDelete.query = 'Select Id From Case Where IsTrueClosed__c = true  and OwnerId not in :queueIdSet and CreatedDate < '+ clearDateString   ;
        ID batchProcessId = Database.executeBatch(batchDelete, 100);
        System.debug('Returned Batch Process ID: ' + batchProcessId);           
        
        System.debug('ClearHistoricalCaseScheduler Stop...'); 
    } 
}
Raj VakatiRaj Vakati
Use this code
 
@isTest
public class testClearHistoricalCaseScheduler{  

    public static testmethod void first1(){
        Test.startTest();
        Datetime dt = Datetime.now().addMinutes(1);
        String CRON_EXP = '0 '+ dt.minute() + ' * ' + dt.day() + ' ' + dt.month() + ' ? ' + dt.year();
       String jobId =   System.schedul('Sample_Heading', CRON_EXP, new ClearHistoricalCaseScheduler  () );   
	    
            CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime
                              FROM CronTrigger WHERE id = :jobId];
            System.assertEquals(CRON_EXP, 
                                ct.CronExpression);
								
								
								
        Test.stopTest();
    }  

}