I have a batch class which is called by an scheduler class which is scheduling itself. I have written a test class which has two methods: One tests the batch and the other the scheduler.Overall the test passed and the batch class code coverage is good and the scheduler class's second method(execute method) is also covered in test class but the first method which is there to schedule itself is not getting covered. Please help. My scheduler class code:

global class ContUpdateScheduler implements Schedulable{
    global static String cronExp = '0 0 13 * * ? *';
    **This method is not getting covered**
    global static String scheduleMe(){
        ContUpdateScheduler scheduler = new ContUpdateScheduler();
        return System.schedule('Update Contacts', cronExp, scheduler);

    global void execute(SchedulableContext sc){
        ContactUpdateBatch batchable = new ContactUpdateBatch();
        Database.executeBatch(batchable, 100);

My testclassmethod for testing the scheduler:

    public static void testSchedule() {
        String sch = '0 0 13 * * ? *';
        ContUpdateScheduler testsche = new ContUpdateScheduler();
        //string fetchID = testsche.scheduleMe();
        string JobId = system.schedule('Test status Check', sch, testsche );

  • The method you want to test is commented out in your test class. – Sebastian Kessel Apr 1 '20 at 15:30
  • @SebastianKessel- I commented it as this line was giving me error: Static method cannot be referenced from a non static context: String If this error would not have appeared I was thinking of adding these two lines: string ID = [SELECT Id, Status FROM AsyncApexJob WHERE ID = :JobId ]; system.assertEquals(ID,fetchID); – Aditya Apr 1 '20 at 15:32
  • 1
    You were just calling it incorrectly, and that was your real error. @sfdcfox already posted the correct answer below. – Sebastian Kessel Apr 1 '20 at 15:33

Your unit test needs to call the method:

@isTest static void testScheduleMe() {

Coincidentally, this will cover the execute method as well.

  • Thank you so much. It solved my problem in a jiffy and I was trying and googling it for so long. Silly me!! calling an static method in this way.. – Aditya Apr 1 '20 at 15:41
  • Unit test with no assertions. >:( – Adrian Larson Apr 1 '20 at 16:17
  • @AdrianLarson tbh, I prefer "only as much assertion as necessary." In this case, if the schedule call fails, an exception is thrown and the test fails. No assertion is strictly necessary. I know others have differing opinions, but I've always been a minimalist with coding. – sfdcfox Apr 1 '20 at 16:33
  • I guess it depends on the purpose of the test. If it is to test that scheduleMe doesn't throw an exception that's fine. But if it wants to check that scheduleMe schedules execution, which in turn calls execute, then some assertions are warranted. – Phil W Apr 1 '20 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.