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
coolkrishcoolkrish 

System.abortjob

Hi,

I am trying to automate cleaning and creating batch jobs as part of post sandbox refresh activity.
I am unable to get code coverage for the line "System.abortJob()"
Code below:

global class GRScheduledJobsAutomation implements SandboxPostCopy { 
    global GRScheduledJobsAutomation() { }
    global void runApexClass(SandboxContext context) { 
    
    // Delete all the gRoster batch jobs
    List<CronJobDetail> cjDtlLst = [SELECT Id FROM CronJobDetail WHERE Name LIKE 'GR%'];        
    List<CronTrigger> ctLst = [SELECT Id FROM CronTrigger WHERE CronJobDetailId IN :cjDtlLst];    
    for(CronTrigger ct:ctLst){
        System.abortJob(ct.Id);
    }
        
    //Create gRoster batch jobs
    RR_SCHED_EmpTerrAct at= new RR_SCHED_EmpTerrAct();
    String cronStr1 = '0 55 * * * ?';
    System.schedule('GR 1', cronStr1, at);
    
    RR_SCHED_ActEmpRole ae= new RR_SCHED_ActEmpRole ();
    String cronStr2 = '0 0 01 * * ?';
    System.schedule('GR 2', cronStr2, ae);
    
    RR_SCHED_VacEmpTerrCrea vt= new RR_SCHED_VacEmpTerrCrea();
    String cronStr3 = '0 0 01 ? * 1-7';
    System.schedule('GR 3', cronStr3, vt);             
       
}
 
}

Test class:
@isTest
public class GR_SCHED_Jobs_Auto_Test {
public static testMethod void testSandboxPostCopyScript() {
 
    GRScheduledJobsAutomation GRJobAuto = new GRScheduledJobsAutomation(); 
    Test.testSandboxPostCopyScript(GRJobAuto, null, null, null); 
    System.assertEquals(3, [SELECT count() FROM CronJobDetail]);       
    
}
}

Please advise how to get code coverage to that line.

Thanks,
Krishna.
 

 
Best Answer chosen by coolkrish
Raj VakatiRaj Vakati
Hi Krishana ,

Code is here 


@isTest
public class GR_SCHED_Jobs_Auto_Test {
    public static testMethod void testSandboxPostCopyScript() {
        RR_SCHED_EmpTerrAct at= new RR_SCHED_EmpTerrAct();
        String cronStr1 = '0 55 * * * ?';
        System.schedule('GR 11', cronStr1, at);
        
        RR_SCHED_ActEmpRole ae= new RR_SCHED_ActEmpRole ();
        String cronStr2 = '0 0 01 * * ?';
        System.schedule('GR 21', cronStr2, ae);
        
        RR_SCHED_VacEmpTerrCrea vt= new RR_SCHED_VacEmpTerrCrea();
        String cronStr3 = '0 0 01 ? * 1-7';
        System.schedule('GR 31', cronStr3, vt);             
        
        GRScheduledJobsAutomation GRJobAuto = new GRScheduledJobsAutomation(); 
        Test.testSandboxPostCopyScript(GRJobAuto, null, null, null); 
        System.assertEquals(3, [SELECT count() FROM CronJobDetail]);       
        
    }
}

 

All Answers

Raj VakatiRaj Vakati
Hi Krishana ,

Code is here 


@isTest
public class GR_SCHED_Jobs_Auto_Test {
    public static testMethod void testSandboxPostCopyScript() {
        RR_SCHED_EmpTerrAct at= new RR_SCHED_EmpTerrAct();
        String cronStr1 = '0 55 * * * ?';
        System.schedule('GR 11', cronStr1, at);
        
        RR_SCHED_ActEmpRole ae= new RR_SCHED_ActEmpRole ();
        String cronStr2 = '0 0 01 * * ?';
        System.schedule('GR 21', cronStr2, ae);
        
        RR_SCHED_VacEmpTerrCrea vt= new RR_SCHED_VacEmpTerrCrea();
        String cronStr3 = '0 0 01 ? * 1-7';
        System.schedule('GR 31', cronStr3, vt);             
        
        GRScheduledJobsAutomation GRJobAuto = new GRScheduledJobsAutomation(); 
        Test.testSandboxPostCopyScript(GRJobAuto, null, null, null); 
        System.assertEquals(3, [SELECT count() FROM CronJobDetail]);       
        
    }
}

 
This was selected as the best answer
coolkrishcoolkrish
Thanks RajaMohan.Spot on.... 
I just created one scheduled job and it is good.