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
Lakshmi SLakshmi S 

Test Class for Schedulable class

HI All,

How to write a test class for schedule apex class?
give explanation about this variable, String sch = '0 0 2 * * ?';
Best Answer chosen by Lakshmi S
Vasani ParthVasani Parth
Lakshmi,

String constant dailyCronExpression = '0 0 2 * * ?' means 'every day at 2 a.m.'

Lets say for example your schedular class is
global with sharing class Batchaccountcountfieldschedule implements Schedulable 
{
    global void execute(SchedulableContext sc)    
    {
       Batchaccountcountfield bb = new Batchaccountcountfield();
       database.executebatch(bb);
    }
}
Then your test class would be,
 
Test.startTest();
Batchaccountcountfieldschedule sh1 = new Batchaccountcountfieldschedule();
String sch = '0 0 2 * * ?'; 
system.schedule('Test Territory Check', sch, sh1); 
Test.stopTest();
// add system asserts to check your expected behaviour

Please mark this as the best answer if this helps
 

All Answers

Ashwani PradhanAshwani Pradhan
This stands for -
Seconds Minutes Hours Day_of_month Month Day_of_week optional_year

'0 0 2 * * ?' This means job scheduled run in every 2 hours.
Ishwar ShindeIshwar Shinde
Hi,

The call to System.schedule is included within the Test.startTest and Test.stopTest block. This ensures that the job gets executed after the Test.stopTest call regardless of the schedule specified in the cron expression. Any asynchronous code included within Test.startTest and Test.stopTest gets executed synchronously afterTest.stopTest.

Please refer the exmaple metioned in below url for test class- 

https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex_scheduling_2.htm

To read crom expression you can visit - http://www.cronmaker.com/
This will help you to determine the next run date as per your crom expression


 
Vasani ParthVasani Parth
Lakshmi,

String constant dailyCronExpression = '0 0 2 * * ?' means 'every day at 2 a.m.'

Lets say for example your schedular class is
global with sharing class Batchaccountcountfieldschedule implements Schedulable 
{
    global void execute(SchedulableContext sc)    
    {
       Batchaccountcountfield bb = new Batchaccountcountfield();
       database.executebatch(bb);
    }
}
Then your test class would be,
 
Test.startTest();
Batchaccountcountfieldschedule sh1 = new Batchaccountcountfieldschedule();
String sch = '0 0 2 * * ?'; 
system.schedule('Test Territory Check', sch, sh1); 
Test.stopTest();
// add system asserts to check your expected behaviour

Please mark this as the best answer if this helps
 
This was selected as the best answer
Pallavi singhPallavi singh
Hi ,
How to write the test class to the schedule class.
This is my schedule class.
global class ScheduleAccount implements Schedulable{
    
    global void execute (Schedulablecontext sc)
    {
        try
           
        {
            
            If (Deletion_Batch_Toggle__mdt.getInstance('AccountCompleteDeletionBatch').Toggle__c){
                            
               AccountCompleteDeletionBatch acd = new AccountCompleteDeletionBatch(); 
            database.executeBatch(acd);
                
            }
            If (Deletion_Batch_Toggle__mdt.getInstance('CasePartialDeletionBatch').Toggle__c){
                
           
            CasePartialDeletionBatch cpd = new     CasePartialDeletionBatch(); 
            database.executeBatch(cpd);
                
             }
            If (Deletion_Batch_Toggle__mdt.getInstance('EventdetailPartialDeletionBatch').Toggle__c){
                
            EventdetailPartialDeletionBatch epd = new EventdetailPartialDeletionBatch(); 
            database.executeBatch(epd);
                
            }
            If (Deletion_Batch_Toggle__mdt.getInstance('SalesCoachingCompleteDeletionBatch').Toggle__c){
                 
            SalesCoachingCompleteDeletionBatch scd = new SalesCoachingCompleteDeletionBatch();
            database.executeBatch(scd);
                
            }
            If (Deletion_Batch_Toggle__mdt.getInstance('WeeklyReportsCompleteDeletionBatch').Toggle__c){
                
            WeeklyReportsCompleteDeletionBatch wcd = new WeeklyReportsCompleteDeletionBatch(); 
            database.executeBatch(wcd);
                
             }
            If (Deletion_Batch_Toggle__mdt.getInstance('EventTasksCompleteDeletionBatch').Toggle__c){ 
                
            EventTasksCompleteDeletionBatch etd = new EventTasksCompleteDeletionBatch(); 
            database.executeBatch(etd);
                
             }
             If (Deletion_Batch_Toggle__mdt.getInstance('TrainingEventCompleteDeletionBatch').Toggle__c){
                 
            TrainingEventCompleteDeletionBatch ted = new TrainingEventCompleteDeletionBatch();
            database.executeBatch(ted);
                 
            }
             If (Deletion_Batch_Toggle__mdt.getInstance('TrainingEventParticipantCompleteDeletion').Toggle__c){
                 
            TrainingEventParticipantCompleteDeletion  tepd = new TrainingEventParticipantCompleteDeletion();
            database.executeBatch(tepd);
                 
             }
        }
        catch(exception ex)
        {
            system.debug(ex.getMessage());
        }
    }
        
}

thank you in advance