I was doing the trailhead schedulable module and I was able to get all the points for the trail, but when I ran the unit test for it with my assertion, I was getting some strange behavior.
Here is the class:
global class DailyLeadProcessor implements Schedulable {
global void execute(SchedulableContext sc) {
List<Lead> recordsToUpdate = new List<Lead>();
List<Lead> leadList = [select Id, LeadSource from Lead where LeadSource = null limit 200];
if(!leadList.isEmpty()) {
for(Lead l : leadList) {
l.LeadSource = 'Dreamforce';
recordsToUpdate.add(l);
}
}
update recordsToUpdate;
}
}
Here is the test class:
@isTest
private class DailyLeadProcessorTest {
static List<Lead> testLeads;
@testSetup
static void initialize() {
testLeads = new List<Lead>();
for(Integer i=0; i<200; i++) {
testLeads.add(new Lead(lastname = 'Smith'+i, company = 'Acme'+i));
}
insert testLeads;
system.assertEquals(200, testLeads.size());
for(Lead l : testLeads) {
system.assertNotEquals(null, l.Id);
system.assertEquals(null, l.LeadSource);
}
}
static testMethod void testSchedulableDailyLeadProcessor() {
initialize();
system.assertEquals(200, testLeads.size());
//[seconds][minutes][hours][Day_of_Month][Month][Day_of_Week][Optional_Year]
String sch = '0 0 0 15 3 ? 2022';
test.startTest();
Id jobId = system.schedule('LeadSchedulerTest', sch, new DailyLeadProcessor());
List<Lead> leadsBefore = [select Id from Lead where LeadSource = null AND Id in: testLeads];
system.assertEquals(200, leadsBefore.size(),'all leads with leadSource are not null');
test.stopTest();
List<Lead> leadsAfter = [select Id, LeadSource from Lead where Id in: testLeads];
for(Lead l : leadsAfter) {
//system.assertEquals('Dreamforce', l.LeadSource,'LeadSource does not contain correct value');
}
}
}
The first strange behavior I encountered was the query that shows the count of leads before the process runs. I was getting a count of 400 instead of 200? I added the additional criteria of AND ID IN: testLeads
and then the size of the list was 200, but I have no clue where those other 200 records came from. There are only 200 records that are created in the test class and there are only 22 leads that are in the trailhead playground org? So, I don't know where the other 200 records came from?
Secondly, the assertion for the leadsAfter list kept failing with some of the records indicating that LeadSource was null. The class was covered at 100% so I can't identify why some records were indicating LeadSource was null, when it should have the value Dreamforce
which is what the schedulable class does.
I ran the check for the module and got the 500 points after commenting out the assertion, but I'm still wondering if there is anything wrong with the code? Or, is it something I didn't catch or is this some odd bug in the org? Anyone else seen this happen or encounter something similar?