Created
October 6, 2011 14:02
-
-
Save richardvanhook/1267464 to your computer and use it in GitHub Desktop.
Apex code showing unit tests both schedule and batch interfaces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.