system.IsScheduled() and Test Classes

This seems wrong to me, but maybe I am missing something simple…

I have this class:

global class GoogleAnalyticsImportScheduled implements Schedulable{
global void execute(SchedulableContext sc) {

Which is tested with this test method:

    GoogleAnalyticsImportScheduled sh = new GoogleAnalyticsImportScheduled();
    String sch = '0 0 8 * * ?';
    system.schedule('GA DATA Import Test', sch, sh);

That test method correctly calls GoogleAnalyticsScheduledUpdates, but the debug in that class says isScheduled() is false. Shouldn’t it be TRUE, even in this test context?


From Testing the Apex Scheduler docs: (my emphasis)

All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously.

So it would appear they aren’t truly being run in the standard async fashion, which may explain why System.IsScheduled() is returning false. It was invoked from the system collected calls rather than from an actual schedule.

That said, I agree that is is confusing. Maybe it is a bug?

I did find System.isBatch() always false, which mentioned System.isScheduled() as well. That was back in 2012.

I don’t know how it is implemented in practice, but I imagine Scheduled jobs run much like other Asynchronous jobs. There is a diagram in the Asynchronous Processing in doc that shows jobs being passed out to various application servers for processing. In an automated test case it would be miles easier to keep it all on the same server and run them synchronous so the progress and outcome could be monitored.

enter image description here

Source : Link , Question Author : BritishBoyinDC , Answer Author : Daniel Ballinger

Leave a Comment