Skip to content

Instantly share code, notes, and snippets.

@richardvanhook
Created October 6, 2011 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardvanhook/1267464 to your computer and use it in GitHub Desktop.
Save richardvanhook/1267464 to your computer and use it in GitHub Desktop.
Apex code showing unit tests both schedule and batch interfaces
global class QuickTest implements Schedulable, Database.Batchable<SObject>{
//schedule hooks
global void execute(SchedulableContext sc){ Database.executeBatch(this); }
//batch hooks
global Database.QueryLocator start(Database.BatchableContext context){
return Database.getQueryLocator([select id from account]);
}
global void execute(Database.BatchableContext context, List<SObject> records){ System.debug('ola'); }
global void finish(Database.BatchableContext context){}
static testmethod void testSchedule(){
Test.startTest();
final String jobId = System.schedule('QuickTest','0 0 0 3 9 ? 2022',new QuickTest());
Test.stopTest();
System.assertEquals('0 0 0 3 9 ? 2022',[select CronExpression from CronTrigger where id = :jobId].CronExpression);
}
static testmethod void testBatch(){
Test.startTest();
(new QuickTest()).execute(null);
Test.stopTest();
//verify batch job actually did something
}
}
@richardvanhook
Copy link
Author

Doesn't look like Test.stopTest() is clever enough to wait on both scheduled apex and batch apex to complete within one unit test, hence this example. Tried multiple API versions up to 23 and no luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment