+ Start a Discussion
Sohan ShirodkarSohan Shirodkar 

Apex scheduler runs after assert statement

I am writing a test class for apex schedular which schedules a batch class to send out emails. Following is the code snippet:
Test.startTest();
String CRON = '0 0 9 * * ?';
String jobId = System.schedule('Lead Followup Reminder Emails Schedular', CRON, new LeadFollowupBatchSchedular());
Test.stopTest();
System.debug('After stopTest');
System.assertEquals(3,LeadFollowupBatch.emailCounter);
//LeadFollowupBatch.emailCounter is a @testVisible variable from batch class scheduled by apex, whose value is set within finish() of that batch
As per documentation, schedule apex will run immediately after Test.stopTest() synchronously. In my case, it is running after assert statements, because of which assert statements are failing. 

I have a System.debug() in batch class finish method. The content of that debug statement appears after "After stopTest', which certainly means scheduler class is not scheduled properly. Even if I check the state of scheduled job using jobId, it returns 'Waiting' and not 'Complete'.

 
Sohan ShirodkarSohan Shirodkar
What could be the reason? Please help