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
hmoh1920hmoh1920 

implement test class to schedule!

Hi,

i coded in this schedule class and i add in my test class, but  i have 0% couverd in my schedule class!

 

this is my  schedule:

global class Sch01 implements Schedulable{


   global void execute(SchedulableContext sc) {


       

      AccountOwnerReassignment acc = new AccountOwnerReassignment();
     database.executebatch(acc);

   }
    
}

 

and this is my test class:

 

@isTest
private class test {

public static testmethod void t63()
{
Sch01 sco = new Sch01();

String sch = '0 0 23 * * ?';

system.schedule('Contract Creates', sch, sco);    
}
}

 

please, where is the problem?

 

thanks.

 

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

Here is a test class for scheduled apex classes.

 

modify to suit your needs

 

static testmethod void test() {
   Test.startTest();

   // Schedule the test job 
      String jobId = System.schedule('testBasicScheduledApex',
      YOURSCHEDULEDAPEXCLASSNAME.CRON_EXP, 
         new YOURSCHEDULEDAPEXCLASSNAME());
   // 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(YOURSCHEDULEDAPEXCLASSNAME.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();

   }

 

Put this line in your Scheduled apex class

 

 global class YOURSCHEDULEDAPEXCLASSNAME implements Schedulable{
   
   //for test method
   public static String CRON_EXP = '0 0 0 3 9 ? 2022';
   
   global void execute(SchedulableContext sc) {
      YOURBATCHCLASSNAME b = new YOUBATCHCLASSNAME('User'); 
      database.executebatch(b, 50);
   }

 

All Answers

AmitSahuAmitSahu

Add these lines to test class:

 


 AccountOwnerReassignment acc = new AccountOwnerReassignment();

sch.execute(acc );


hmoh1920hmoh1920

i have this error:

 

Save error: Method does not exist or incorrect signature: [String].execute(BalanceCalculBatch)

 

at level :sch.execute(acc );

 

Starz26Starz26

Here is a test class for scheduled apex classes.

 

modify to suit your needs

 

static testmethod void test() {
   Test.startTest();

   // Schedule the test job 
      String jobId = System.schedule('testBasicScheduledApex',
      YOURSCHEDULEDAPEXCLASSNAME.CRON_EXP, 
         new YOURSCHEDULEDAPEXCLASSNAME());
   // 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(YOURSCHEDULEDAPEXCLASSNAME.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();

   }

 

Put this line in your Scheduled apex class

 

 global class YOURSCHEDULEDAPEXCLASSNAME implements Schedulable{
   
   //for test method
   public static String CRON_EXP = '0 0 0 3 9 ? 2022';
   
   global void execute(SchedulableContext sc) {
      YOURBATCHCLASSNAME b = new YOUBATCHCLASSNAME('User'); 
      database.executebatch(b, 50);
   }

 

This was selected as the best answer
hmoh1920hmoh1920

I have 25% of my schedule class covered! ;only  this ligne  ( public static String CRON_EXP = '0 0 0 3 9 ? 2022';)

the other lignes is not covered!

 


you have a solution?

 

 

thanks.

Starz26Starz26

Cannot do anything with seeing your code.

 

Post:

 

1. Name of your batch class

2. Code of your batch schedule class

3. Code of the test class

 

 

hmoh1920hmoh1920

Thanks, i have rename my test class and add   Test.startTest(); and  Test.stopTest();  to my code, now i have 79% covered.

 thank you.