function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
G2WIntegrationG2WIntegration 

Bug? Activating a scheduler in a test now fires the execute method as well in that test

It seems that as of Winter '12, there was a change in how tests handle scheduled jobs.

 

Before Winter '12, we had code that woke up a scheduler, but the execute method wasn't fired.

 

After Winter '12, that same code fails as the scheduler is now firing the execute method (which happens to create 2nd batch job in the same test).

 

Bug or expected behavior?

 

Example:

test.startTest();                    
  system.schedule('Some scheduled job', varForStartTime, new someSchedulerClass()); 
test.stopTest();

 

global with sharing class someSchedulerClass implements Schedulable{
    global void execute(SchedulableContext ctx) {
		someMethodThatFiresBatchJob();
    }//execute
}

 Removing test.startTest() and test.stopTest() fixes the problem as then the execute method doesn't fire.  

Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

From the SFDC Apex doc:

Testing the Apex Scheduler
The following is an example of how to test using the Apex scheduler.
The System.schedule method starts an asynchronous process. This means that when you test scheduled Apex, you must ensure that the scheduled job is finished before testing against the results. Use the Test methods startTest and stopTest around the System.schedule method to ensure it finishes before continuing your test. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously. If you don’t include the System.schedule method within the startTest and stopTest methods, the scheduled job executes at the end of your test method for Apex saved using Salesforce.com API version 25.0 and later, but not in earlier versions.

 Winter 13 (I presume you meant Winter 13, not Winter 12)  is V26.0

 

I haven't noticed any changes as my Scheduled classes were written well before V26.0 and continue to execute correctly in Winter 13

 

I've never tried to schedule two identical jobs (same schedulable class) in the same testmethod

All Answers

crop1645crop1645

From the SFDC Apex doc:

Testing the Apex Scheduler
The following is an example of how to test using the Apex scheduler.
The System.schedule method starts an asynchronous process. This means that when you test scheduled Apex, you must ensure that the scheduled job is finished before testing against the results. Use the Test methods startTest and stopTest around the System.schedule method to ensure it finishes before continuing your test. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously. If you don’t include the System.schedule method within the startTest and stopTest methods, the scheduled job executes at the end of your test method for Apex saved using Salesforce.com API version 25.0 and later, but not in earlier versions.

 Winter 13 (I presume you meant Winter 13, not Winter 12)  is V26.0

 

I haven't noticed any changes as my Scheduled classes were written well before V26.0 and continue to execute correctly in Winter 13

 

I've never tried to schedule two identical jobs (same schedulable class) in the same testmethod

This was selected as the best answer
G2WIntegrationG2WIntegration

Hmm-so I guess its always fired the execute method as long as the scheduler is run within test.startTest/stopTest(), but now that we changed the target method to a batch job, the error occured.

 

Thanks for the response.