How can test this class?

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

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


It may not be considered best practice, but I find with batch and schedulable classes it's way easier to just run the methods inside them manually, and this also lets you run several in a test and check the results of the runs without running into difficulty:

SchedulableContext sc = null;
TheScheduleableClass tsc = new TheScheduleableClass();
  • 8
    That's not the best practice, or even the recommended practice. The other answers here are correct, while this one is not.
    – sfdcfox
    Oct 4 '13 at 22:06
  • 2
    Hey, I said it wasn't the best way to do it ;) To my mind though it still tests the functionality of the class; I don't feel the need to test that the system scheduler will fire it or not when I know it works from manual testing. These methods should compliment manual tests, not replace them.
    – Matt Lacey
    Oct 5 '13 at 0:37
  • 2
    Actually, the point of test methods is to replace manual testing. In theory, developers should be able to change their production code and verify that the changes do not break the intended logic, without manual testing. Anyone not using proper test methods will spend up to 1000% more time testing their code manually.
    – sfdcfox
    Oct 5 '13 at 2:44
  • 4
    While it's true that not all code can be tested automatically, those should be limited to as few cases as possible. Since we have a correct method for testing scheduled classes, we should use those proper methods. They are documented and work perfectly.
    – sfdcfox
    Oct 5 '13 at 2:50
  • 3
    I'm not going to cover fringe cases. Like you said, it could take a while. However, like any good developer, I'm always going to recommend the method that the platform developers themselves recommend and have designed the system for. If, in some future release, the SC parameter has some use, then this method of testing won't work. Best to avoid the habit to begin with.
    – sfdcfox
    Oct 5 '13 at 3:00

If you just want code coverage for this scheduled class then you can do something like below, but you still should have a test method that tests the logic for your batch class as well.

private class myTestClass {

    static testMethod void myTestMethod() {        
         CustomerPortalRevokeSchedulable myClass = new CustomerPortalRevokeSchedulable ();   
         String chron = '0 0 23 * * ?';        
         system.schedule('Test Sched', chron, myClass);

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.