Test class for schedulable interface

How can test this class?

global class CustomerPortalRevokeSchedulable implements Schedulable {
    
    global void execute(SchedulableContext c)
    {   
        CustomerPortalRevokeBatch theC = new CustomerPortalRevokeBatch();
        Database.executeBatch(theC);                 
    }
}

Answer

To test a schedulable, schedule a run in your test method using system.schedule(), and wrap that call in test.startTest() and test.stopTest() calls. This will ensure that the scheduled job is finished when test.stopTest() completes and the next line of code runs. See Testing the Apex Scheduler in the Force.com Apex Code Developer’s Guide for a full explanation and sample code.

EDIT: to clarify a point I should have made to begin with: your scheduleable class will not execute until test.stopTest() runs. Any assertions before test.stopTest() are testing the initial state; any assertions of your class’s actual behavior (e.g., querying to confirm expected changes) must occur after test.stopTest

Attribution
Source : Link , Question Author : Enry , Answer Author : Jason Clark

Leave a Comment