+ Start a Discussion
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Please help with Apex Schedule Test Class

Hi Guys and girls,

I have recently written my very first scheduled apex class. This is my first one ever, so far it seems to work. I am trying to write a test class, but I am coming with issues, mainly with code coverage. I cannot write  a test capable of passing or if it does pass, it will only test like 24%. 

I was hoping to maybe get some tips on:

* My class, any issues that might present themselves

*Perhaps a method of only adding records that have been changed by class to list for update? or does it not matter really?

*Help to write my test class so that it passes atleast 75%

Please any help would be very much appreciated. Thank you in advance.

This is my scheduled apex class:

 

**
* To schedule the Daily reconciliation:
*    NOTE: It should run at midnight every day on it's own, but if you make
*    changes and need to requeue run the command below from the developer's console
*
*    scheduledMonthly.scheduleIt();
*    NOTE: This class is designed to replace the ServiceSupportReview Time based workflows.
*/
 
global class scheduledDaily_ServiceSupportReviews implements Schedulable {
  public static String CRON_EXP = '0 0 15 * * ? *';

    
    global static String scheduleIt() {
        scheduledDaily_ServiceSupportReviews  sm = new scheduledDaily_ServiceSupportReviews ();
        return System.schedule('Daily Reconciliation', CRON_EXP, sm);
    }

    
    global void execute(SchedulableContext sc) {
        //Code goes here
        
         List<Service__c> Servs = new List<Service__c>();
         
         Date TodayDate = date.today();

         

    for(Service__c S : [SELECT id, Service_Name__c, End_of_Service_Support__c, X30Days_Review__c, X60Days_Review__c, X40DaysBefore_Review__c, X270Days_Review__c, X180Days_Review__c, X90Days_Review__c,  Start_of_Service__c, Account__c FROM Service__c where service_active__c = true]){
    
    If(S.Service_Name__c == 'Intensive Property Coaching'){
     //30 Days support Review boolean
      if ((S.End_of_Service_Support__c).addDays(-335) == date.today() && S.X30Days_Review__c == false) {
        S.X30Days_Review__c = true;
      
    //60 Days support Review boolean  
      } else if ((S.End_of_Service_Support__c).addDays(-305) == date.today() && S.X60Days_Review__c == false) {
        S.X60Days_Review__c = True;
    
    }
    }
    
    If(S.Service_Name__c != 'Essential Property Education Only'){
    //90 Days support Review boolean
      if ((S.End_of_Service_Support__c).addDays(-275) == date.today() && S.X90Days_Review__c == false) {
        S.X90Days_Review__c = true;
      
    //180 Days support Review boolean  
      } else if ((S.End_of_Service_Support__c).addDays(-185) == date.today() && S.X180Days_Review__c == false) {
        S.X180Days_Review__c = True;
        
    //270 Days support Review boolean   
      } else if ((S.End_of_Service_Support__c).addDays(-95) == date.today() && S.X270Days_Review__c == false) {
        S.X270Days_Review__c = true;
    
    //40 Days before end of support
     } else if ((S.End_of_Service_Support__c).addDays(-40) == date.today() && S.X40DaysBefore_Review__c == false) {
        S.X40DaysBefore_Review__c = true;
 
      }
      }
      
      //Add altered services to a list
      
    
      Servs.add(S);
     
    }
    
    
    if (!Servs.isEmpty()) {   
    update Servs;
 }
    
    
    
}
}
 



This is my attempt at a test:
(Currently getting error - System.AsyncException: Based on configured schedule, the given trigger will never fire.

@isTest
                                
private class TestSchedulableClass {

   // CRON expression: midnight on March 15.
   // Because this is a test, job executes
   // immediately after Test.stopTest().
   public static String CRON_EXP = '0 0 15 15 03 ? 2015';

   static testmethod void test() {
   
   Account acc = new Account(Name = 'Test Schedule Account');
    insert acc;
   Service__c serTest = new Service__c(Account__c = acc.id);
      Test.startTest();

      // Schedule the test job
      String jobId = System.schedule('ScheduleApexClassTest',
                        CRON_EXP, 
                        new scheduledDaily_ServiceSupportReviews());
         
      // 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(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('2015-03-15 15:00:00', 
         String.valueOf(ct.NextFireTime));
      // Verify the scheduled job hasn't run yet.
      Service__c [] S = [SELECT id, Service_Name__c, End_of_Service_Support__c, X30Days_Review__c, X60Days_Review__c, X40DaysBefore_Review__c, X270Days_Review__c, X180Days_Review__c, X90Days_Review__c,  Start_of_Service__c, Account__c FROM Service__c where service_active__c = true];
      System.assertEquals(S.size(),0);
      Test.stopTest();

      // Now that the scheduled job has executed after Test.stopTest(),
      //   fetch the new merchandise that got added.
     // System.assertEquals(ml.size(), 1);

   }
   
   }
 



 

Best Answer chosen by Developer.mikie.Apex.Student
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Developer.mikie.Apex.Student,

Run Your Test Class then GO to Developer Console,In Down panel,there is a tab calles 'Tests', click on your test class and in right side panel you can see 'Overall Code Coverage'. Click on your class name and you will get red lines for uncovered code.

Let us know if it helps.

 

All Answers

Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi  Developer.mikie.Apex.Student,

You have defined past date in below line,Please define future date here.
 
public static String CRON_EXP = '0 0 15 15 03 ? 2015';

Let us know if it helps you.
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Thank you for the speedy reply. It now passes, however it does not pass with enough code coverage. Do you know what information i need to add to my test class to get a 75-100% pass? Thank you for your time.
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Developer.mikie.Apex.Student,

Please Mark it as BEST ANSWER so others can get help from it.
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC

Hi Developer.mikie.Apex.Student,

Could you please highlight which all code is not getting passed ?
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Hi There, I am not 100 % sure how to do that?
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Hi There, I managed to get 88% code coverage, I had to write the code anew however, the old code would not get above 24%.

Any tips on how I might get the last 12 % or any tips on making the test run smoother would be much appreciated.


 

@istest
class scheduled_ServiceSupportReviews_Test{

   static testmethod void testSchedule() {
   
    List<Service__c> Servs = new List<Service__c>();
   
   
    Account acc = new Account(Name = 'Test Schedule Account');
    insert acc;
    
    
    Service__c S1 = new Service__c ();
      S1.Account__c = acc.id;
      S1.Service_Name__c = 'Intensive Property Coaching';
      S1.Start_of_Service__c = date.today().addDays(-40);
      S1.End_of_Service_Support__c  = date.today().addDays(40);
      servs.add(s1);
      
      Service__c S2 = new Service__c ();
      S2.Account__c = acc.id;
      S2.Service_Name__c = 'Intensive Property Coaching';
      S2.Start_of_Service__c = date.today().addDays(-275);
      S2.End_of_Service_Support__c  = date.today().addDays(275);
      servs.add(s2);
      
      
       Service__c S3 = new Service__c ();
      S3.Account__c = acc.id;
      S3.Service_Name__c = 'Intensive Property Coaching';
      S3.Start_of_Service__c = date.today().addDays(-335);
      S3.End_of_Service_Support__c  = date.today().addDays(335);
      servs.add(s3);
      
       Service__c S4 = new Service__c ();
      S4.Account__c = acc.id;
      S4.Service_Name__c = 'Intensive Property Coaching';
      S4.Start_of_Service__c = date.today().addDays(-305);
      S4.End_of_Service_Support__c  = date.today().addDays(305);
      servs.add(s4);
      
       Service__c S5 = new Service__c ();
      S5.Account__c = acc.id;
      S5.Service_Name__c = 'Intensive Property Coaching';
      S5.Start_of_Service__c = date.today().addDays(-185);
      S5.End_of_Service_Support__c  = date.today().addDays(185);
      servs.add(s5);
      
           Service__c S6 = new Service__c ();
      S6.Account__c = acc.id;
      S6.Service_Name__c = 'Intensive Property Coaching';
      S6.Start_of_Service__c = date.today().addDays(-95);
      S6.End_of_Service_Support__c  = date.today().addDays(95);
      servs.add(s6);
      
        Service__c S7 = new Service__c ();
      S7.Account__c = acc.id;
      S7.Service_Name__c = 'Essential Property Education Only';
      S7.Start_of_Service__c = date.today().addDays(-185);
      S7.End_of_Service_Support__c  = date.today().addDays(185);
      servs.add(s7);
      
      
      insert Servs;
    
    
   Test.startTest();


      

      // Schedule the test job

      String jobId = System.schedule('testBasicScheduledApex',
      scheduledDaily_ServiceSupportReviews.CRON_EXP, 
         new scheduledDaily_ServiceSupportReviews());

      // 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(scheduledDaily_ServiceSupportReviews.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('2015-04-25 01:30:00', 
         String.valueOf(ct.NextFireTime));
         
      //System Asserts
      //40 days before
      System.assertNotEquals(True,
         [SELECT id, X40DaysBefore_Review__c FROM Service__c WHERE  id =: s1.id].X40DaysBefore_Review__c);
      //90 days review
       System.assertNotEquals(True,
         [SELECT id, X90Days_Review__c FROM Service__c WHERE  id =: s2.id].X90Days_Review__c);
         //30 days review
       System.assertNotEquals(True,
         [SELECT id, X30Days_Review__c FROM Service__c WHERE  id =: s3.id].X30Days_Review__c);
         //60 days review
       System.assertNotEquals(True,
         [SELECT id, X60Days_Review__c FROM Service__c WHERE  id =: s4.id].X60Days_Review__c);
         //180 days review
       System.assertNotEquals(True,
         [SELECT id, X180Days_Review__c FROM Service__c WHERE  id =: s5.id].X180Days_Review__c);
         //270 days review
       System.assertNotEquals(True,
         [SELECT id, X270Days_Review__c FROM Service__c WHERE  id =: s6.id].X270Days_Review__c);

   Test.stopTest();
   
   //40 Days before
   System.assertEquals(True,
         [SELECT id, X40DaysBefore_Review__c FROM Service__c WHERE  id =: s1.id].X40DaysBefore_Review__c);
   //90 days review
   System.assertEquals(True,
         [SELECT id, X90Days_Review__c FROM Service__c WHERE  id =: s2.id].X90Days_Review__c);
         //30 days review
      
   System.assertEquals(True,
         [SELECT id, X30Days_Review__c FROM Service__c WHERE  id =: s3.id].X30Days_Review__c);
         
         
         //60 days review
   System.assertEquals(True,
         [SELECT id, X60Days_Review__c FROM Service__c WHERE  id =: s4.id].X60Days_Review__c);
         //180 days review
   System.assertEquals(True,
         [SELECT id, X180Days_Review__c FROM Service__c WHERE  id =: s5.id].X180Days_Review__c);
          //270 days review
   System.assertEquals(True,
         [SELECT id, X270Days_Review__c FROM Service__c WHERE  id =: s6.id].X270Days_Review__c);

   }
}
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Developer.mikie.Apex.Student,

Run Your Test Class then GO to Developer Console,In Down panel,there is a tab calles 'Tests', click on your test class and in right side panel you can see 'Overall Code Coverage'. Click on your class name and you will get red lines for uncovered code.

Let us know if it helps.

 
This was selected as the best answer