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
AntonPavlovAntonPavlov 

help to solve, write test apex schedular


here apex code 
global with sharing class schedularCloseAllDealsMonthAgo implements Schedulable{
      global void execute(SchedulableContext sc) {  
            closeAllDealsMonthAgo closeDeals = new closeAllDealsMonthAgo(); 
            database.executebatch(closeDeals);
      }
      public static void schedulerMethod(){
            schedularCloseAllDealsMonthAgo sched = new schedularCloseAllDealsMonthAgo();
            string con_exp= '0 0 10 * * ?';
            System.schedule('schedularCloseAllDealsMonthAgo', con_exp, sched);
      }
}
This is code of test but not work for 100%
@isTest   
public class schedularCloseAllDealsMonthAgoTest {
    public static testMethod void testschedule() {
        Test.StartTest();
            schedularCloseAllDealsMonthAgo sched = new schedularCloseAllDealsMonthAgo();
            String CRON_EXP = '0 0 23 * * ?';
            System.schedule('schedularCloseAllDealsMonthAgo', CRON_EXP, sched);
        Test.stopTest();
    }
}
Best Answer chosen by AntonPavlov
SUCHARITA MONDALSUCHARITA MONDAL

Hi Anton,

Change your test class as below:

@isTest   
public class schedularCloseAllDealsMonthAgoTest {
    public static testMethod void testschedule() {
        Test.StartTest();
            schedularCloseAllDealsMonthAgo.schedulerMethod();
        Test.stopTest();
    }
}

 

Hope this helps
Thanks,
Sucharita

 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Anton,

>> http://amitsalesforce.blogspot.com/2017/07/how-to-write-test-class-for-scheduler.html

In the above link, there is an example of a schedulable class, and below is the sample test class that you can use to implement your test class.
@isTest
private class AccountUpdateBatchJobscheduledTest
{

    static testmethod void schedulerTest() 
    {
        String CRON_EXP = '0 0 0 15 3 ? *';
        
        // Create your test data
        Account acc = new Account();
        acc.name= 'test';
        insert acc;
        
        Test.startTest();

            String jobId = System.schedule('ScheduleApexClassTest',  CRON_EXP, new AccountUpdateBatchJobscheduled());
            CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
            System.assertEquals(CRON_EXP, ct.CronExpression);
            System.assertEquals(0, ct.TimesTriggered);

        Test.stopTest();
        // Add assert here to validate result
    }
}


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
SUCHARITA MONDALSUCHARITA MONDAL

Hi Anton,

Change your test class as below:

@isTest   
public class schedularCloseAllDealsMonthAgoTest {
    public static testMethod void testschedule() {
        Test.StartTest();
            schedularCloseAllDealsMonthAgo.schedulerMethod();
        Test.stopTest();
    }
}

 

Hope this helps
Thanks,
Sucharita

 

This was selected as the best answer
AntonPavlovAntonPavlov
Thaks you all you helped a lot.