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
BellaBella 

Apex Scheduler Test

I wonder if anyone can help me with a test method for a class that implements Scheduler. It looks like this:

 

 

global class scheduledCreateOpportunity implements Schedulable{
global void execute(SchedulableContext ctx) {
//get all contracts and make a set of their ids
Map<Id, Contract> contracts = new Map<Id, Contract>([Select Id, Name, Account.PriceLevel__c, Account.Id, Related_SLC__r.Id, ContractTerm, Integrator__r.Id, Distributor__r.Id, End_User__r.Id, Representative__r.Id, EndDate
From Contract
Where EndDate =: Date.today()]);
Set<Id> sContIds = contracts.keySet();
....
}
}

 

 It's a pretty big class, but that the end of the day the idea is that it's supposed to create opportunity from a contract at a certain time based on the contract's end date. I know how to write most of the test method, I just don't know how to call the class above correctly, and the page bellow doesn't seem to help :(

 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_scheduler.htm?SearchType=Stem

 

 I'm not using the CronTrigger, just schedualing this thing to run via front end every day at midnight. Any ideas how I might call the class for testing?

 

Message Edited by Bella on 03-26-2010 07:20 AM
Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

In your test method, set up your test data as usual.

 

Then when you're ready to call the scheduled class, you can use something like this:

 

 

test.starttest();

scheduledCreateOpportunity sco = new scheduledCreateOpportunity();

String sch = '0 0 23 * * ?';

system.schedule('Contract Creates', sch, sco);

test.stopTest();

 

 This is testing on the assumption that the class is scheduled to run every night at 11pm (23:00)

 

This will execute the schedule, and you can then test that the records were updated/created as required 

 

One thing to note - I think they recommend that the actual code you're executing is contained in a separate class, that you just create and execute in the Scheduled Class... 

 

Message Edited by BritishBoyinDC on 03-26-2010 12:55 PM
Message Edited by BritishBoyinDC on 03-26-2010 12:55 PM

All Answers

BritishBoyinDCBritishBoyinDC

In your test method, set up your test data as usual.

 

Then when you're ready to call the scheduled class, you can use something like this:

 

 

test.starttest();

scheduledCreateOpportunity sco = new scheduledCreateOpportunity();

String sch = '0 0 23 * * ?';

system.schedule('Contract Creates', sch, sco);

test.stopTest();

 

 This is testing on the assumption that the class is scheduled to run every night at 11pm (23:00)

 

This will execute the schedule, and you can then test that the records were updated/created as required 

 

One thing to note - I think they recommend that the actual code you're executing is contained in a separate class, that you just create and execute in the Scheduled Class... 

 

Message Edited by BritishBoyinDC on 03-26-2010 12:55 PM
Message Edited by BritishBoyinDC on 03-26-2010 12:55 PM
This was selected as the best answer
BellaBella

In what you wrote, in 

system.schedule('Contract Creates', sch, sh);

where does the sh come from? Or was that supposed to be sco, what you called the new instance of the class?

 

haha nevermind, I see it ^_^

Message Edited by Bella on 03-26-2010 10:00 AM
BellaBella
Humm so I tried that and even after prepopulating everything and running the test, eclipse tells me that the execute method is never called so nothing really happens. Dose anyone know what's going on?
BritishBoyinDCBritishBoyinDC

Hmm, that definitely worked for me. Note - the test.stoptest() is what tells the test script to execute the scheduled class, so make sure that is in the right place.

 

As I mentioned, my scheduled class just creates a new instance of a separate class with my actual code in it including the test script), and then calls a method in that class to execute it. So might be worth trying that? 

BellaBella
You're right, I was putting the start and stop of the test in the wrong place ^_^ 90% test coverage now. Thank you so much!
Sam_IndiaSam_India

Hi,

 

Is there any way in SFDC by which I can call a piece of code after every X minutes(say every 2 mins)?

Please help, need urgently. I tried CronKit, but it takes at least one hour.

 

Thanks,

Sam