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
Daniel B ProbertDaniel B Probert 

Schedule Test Class 57%

Hi Anyone able to tell me what's wrong with my test class.

 

here is my schedule:

global class MFHourlyScheduler implements Schedulable{
    global MFHourlyScheduler (){}
    
    public static void start(){
    String mfsch = '0 0 13 * * ?';
        System.schedule('Forms Update', mfsch, New MFHourlyScheduler());
    }

    global void execute(SchedulableContext ctx){
        MFUpdateBatch b = new MFUpdateBatch();
        database.executebatch(b);    
    }
}

 and here is my test class:

 

@istest
class TestMFSchedule {
   static testmethod void test() {
   Test.startTest();
   String mfsch = '0 0 13 * * ?'; 
    system.schedule('Forms Update', mfsch, New MFHourlyScheduler());

   Test.stopTest();
   }
}

 i get it to 57% with this being marked as now being tested:

public static void start(){
String mfsch = '0 0 13 * * ?';
System.schedule('Forms Update', mfsch, New MFHourlyScheduler());

 any ideas appreciated.

 

dan

Starz26Starz26

Here is the pattern I use for scheduled classes and tests. Only will work until the year 2022 though so you may need to update it to be dynamic

 

 

global class CLASSNAME implements Schedulable{
 
   
   //for test method
   public static String CRON_EXP = '0 0 0 3 9 ? 2022';
   
   global void execute(SchedulableContext sc) {
//your code   
   }

 

   @isTest(seeAllData=true)
   static void test() {
   Test.startTest();

   // Schedule the test job 
      CLASSNAME os = new CLASSNAME();
      os.deleteMe = true;
      
      String jobId = System.schedule('testBasicScheduledApex',
      CLASSNAME.CRON_EXP, os);
   // Get the information from the CronTrigger API object 
      CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, 
         NextFireTime
         FROM CronTrigger WHERE id = :jobId];

   // Verify the expressions are the same 
    
      System.assertEquals(RW_OrderDetailSyncScheduler.CRON_EXP, 
         ct.CronExpression);

   // Verify the job has not run 
    
      System.assertEquals(0, ct.TimesTriggered);

   // Verify the next time the job will run 
    
      System.assertEquals('2022-09-03 00:00:00', 
         String.valueOf(ct.NextFireTime));
      

   Test.stopTest();

   }
}