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
Sanu VermaSanu Verma 

How to write CronJobDetails Test Class

Want to Write CronJobDetail Test class
public void abortCronJob(){
            List<CronJobDetail> detailIds = new List<CronJobDetail>([Select Id From CronJobDetail where Name ='ScheduleBatchPaymentDaysPassed']);
    System.debug('++detailsIds++=='+detailIds);
        if(detailIds != null && detailIds.size() > 0){
            Id detailId = detailIds[0].Id; 
            List<CronTrigger> cronTriggerList = new List<CronTrigger>([SELECT Id from CronTrigger WHERE CronJobDetailId = :detailIds[0].Id]);
            if(cronTriggerList.size() > 0){ 
                System.abortJob(cronTriggerList[0].Id); 
            }
        } 
}

 
Ajay K DubediAjay K Dubedi
Hi Sanu,

Please refer the below code. Hope it helps you.

@isTest
private class cornJobDetailTest {
    
    @IsTest static void test()
    {
        
        String CRON_EXP = '0 0 0 15 3 ? *';
        
        Test.startTest();
        Id JobId = system.schedule('ScheduleBatchPaymentDaysPassed',CRON_EXP, //name of your schedule class for eg :- new schedulejobDetailclass());
        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
        System.assertEquals(CRON_EXP, ct.CronExpression);
        System.assertEquals(0, ct.TimesTriggered);
        Test.stopTest();
    }
}

Please select as best answer if it helps you.

Thank You,
Ajay Dubedi
Sanu VermaSanu Verma
Hii ajay, Thnxx For Reply.. I go through with ur code but in " Id JobId = system.schedule('ScheduleBatchPaymentDaysPassed',CRON_EXP,new BatchPaymentDaysPassed());" I got error tht method have incorrect signature. 

can u plzz tell me why is it so...
Ajay K DubediAjay K Dubedi
Hi Sanu,

Try this one.

@IsTest
private class cornJobDetailTest  {
@IsTest static void test()
{
    string cronex = '0 0 0/6 1/1  ? '; // you need to write a corn expression as required.
    ScheduleBatchOpportunity schdel = new ScheduleBatchOpportunity(); // Here you need to write a schedulable class of yours and instatciate it like this.
    Id JobId = system.schedule('Every 6 hrs of schedule',cronex, schdel); // here passs the object of above schedule class in place of "schdel".
     
 Test.startTest();
    //Here you need to instantiate your batch class.
 BatchPaymentDaysPassed batchpayment = new BatchPaymentDaysPassed(); // I hope this is your batch class.
    Test.stopTest();
}
}

Please select as best answer if it helps you.

Thank You,
Ajay Dubedi