+ Start a Discussion

How to write test for Apex Scheduler



I have created a Class that implements the Schedulable interface for a batch Apex class called batchable:



global class scheduledBatchable implements Schedulable{ global void execute(SchedulableContext sc) { batchable b = new batchable(); database.executebatch(b); } }


 Now I am not sure how I can write my test class... Can i call execute() ? What should be the SchedulableContext?





Best Answer chosen by Admin (Salesforce Developers) 

Take a look at the docs: 



Per the example, you can validate the cron expression by validating the next fire time.  Similar to Batch and @future, you can invoke the scheduler after Test.startTest() and assert the execute behavior after Test.stopTest().



Message Edited by tmatthiesen on 03-08-2010 09:29 AM