+ Start a Discussion
Kourt NeyraKourt Neyra 

How to write a test class for a scheduled apex class

Hi, I have an apex scheduled class and i want to deploy it to production, but it has a 0% code coverage, how can i write a test class for it? This is the code:
 
global class BorrarAsegurados implements Schedulable{
    global void execute(schedulablecontext SC){
        List<Enrollee__c> Asegurados = [select id from Enrollee__c where Policy_End_Date__c <= Yesterday ];
        if(!Asegurados.isEmpty())
            delete Asegurados;
    }

}

 
Best Answer chosen by Kourt Neyra
Balayesu ChilakalapudiBalayesu Chilakalapudi
Try like this,
@isTest
public class BorrarAseguradosTest{
     public static testmethod void test1(){
      Test.startTest();
        Enrollee__c en=new Enrollee__c();
        en.Policy_End_Date__c =System.Date.Today()-1;
        insert en;

       // This test runs a scheduled job at midnight Sept. 3rd. 2022
        public static String CRON_EXP = '0 0 0 3 9 ? 2022';
       // Schedule the test job
       String jobId = System.schedule('BorrarAseguradosTest', CRON_EXP, new BorrarAsegurados());
      // 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('2022-09-03 00:00:00', String.valueOf(ct.NextFireTime));   
      Test.stopTest();        
     }
}

Let us know if it helps
 

All Answers

Balayesu ChilakalapudiBalayesu Chilakalapudi
Try like this,
@isTest
public class BorrarAseguradosTest{
     public static testmethod void test1(){
      Test.startTest();
        Enrollee__c en=new Enrollee__c();
        en.Policy_End_Date__c =System.Date.Today()-1;
        insert en;

       // This test runs a scheduled job at midnight Sept. 3rd. 2022
        public static String CRON_EXP = '0 0 0 3 9 ? 2022';
       // Schedule the test job
       String jobId = System.schedule('BorrarAseguradosTest', CRON_EXP, new BorrarAsegurados());
      // 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('2022-09-03 00:00:00', String.valueOf(ct.NextFireTime));   
      Test.stopTest();        
     }
}

Let us know if it helps
 
This was selected as the best answer
Kourt NeyraKourt Neyra
thank you very much, it worked!

The only thing Ihad to change was in line 10, where I had to erase the "Public static" modifiers to the string variable. But now the code coverage is 100% , thanks!

I am analysing this test code and what I understand is that I need to write a code that verifies if the class is working properly, right?

In this case, I needed to create an enrollee__c record that was going to be erased with my class and then I needed to run the class, and for last I needed to check that the record was erased.

thanks!