What do I assert in this test for a scheduled class?

This is my Scheduler class.

global class LeadSummary_Scheduler implements Schedulable{
   global void execute(SchedulableContext sc) 
   {
       getCreatedLeadsSummary gcls = new getCreatedLeadsSummary();
       Database.ExecuteBatch(gcls);
   }
}

The test coverage on this class was 0%. So I started writing a test for this class. Here is my test.

@isTest
private class TestLeadSummarySchedClass {
static testmethod void test() {

    Test.startTest();
        LeadSummary_Scheduler batchApex = new LeadSummary_Scheduler();
        batchApex.execute(null);
    Test.stopTest();
}
}

So now my question is what do I need to assert after Test.stopTest()?

Edit: Interestingly this test has covered the above class by 100%. Is this by design?

Answer

Abhi, scheduled APEX should be tested in a slightly different way.
Please refer to the testing section in this article which will show you how.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

Basically the thing to note is that when you are testing scheduled APEX, you need to ensure that the scheduled method has been executed before you can check the results of your method.

Therefore as shown in the above example, you will need to use System.schedule and schedule a job within the Test.startTest() and Test.stopTest() methods.

You can also see an example of how the Assert statement has been used.

Attribution
Source : Link , Question Author : abhi , Answer Author : Richard N

Leave a Comment