Testing scheduled jobs and Apex scheduling
Scheduled Apex, as discussed, is a form of asynchronous Apex and, therefore, we should utilize the Test.startTest()
and Test.stopTest
methods to ensure that the asynchronous code executes within our test method. Suppose we had the following class for testing:
public class SchedulableExample implements Schedulable { public void execute(SchedulableContext SC) { Account a = new Account(); a.Name = String.valueOf(DateTime.now()); insert a; } }
This code is not overly useful but will allow us to illustrate the testing of Scheduled Apex. If we are scheduling this job using the Apex Scheduler or the System.schedule
method via Execute Anonymous, we can write our test to simply schedule the class at any point and verify the results of the action once the...