I am using FINAL
to declare a constant on my scheduleable
class, which is set to Now
, essentially so that I can know what value to test for in my test class. The value of the constant is written to a text field on a record in a daily batch job, if certain conditions are met.
My unit tests are passing. The tests include a test that the constant is not null and also that the value of the text field on the record after the scheduled job is also not null.
But in prod, i am finding that the result after the job is in fact null. The field was populated before, and after the job ran this morning, its been set to null.
So, is the issue that I cant use a constant on a schedulable class? Or that I need to do something involving static? Or that i'm missing something else completely? And why are my tests passing?
global class WebProfileDailyUpdate implements Schedulable {
public FINAL datetime EXECTIME = datetime.now();
global void execute(SchedulableContext ctx) {
//string s = string.valueof(Datetime.now());
list<web_profile__c> wp = new list<web_profile__c>();
for (web_profile__c w : [select Id, Temp_Dummy__c FROM web_profile__c WHERE (Contact__r.LastModifiedDate >= YESTERDAY OR Displayed_Organization__r.LastModifiedDate >= YESTERDAY OR Elected_Organization__r.LastModifiedDate >= YESTERDAY OR Opportunity__r.LastModifiedDate >= YESTERDAY)]) {
system.debug('wp size = ' + wp.size());
w.temp_dummy__c = string.valueOf(EXECTIME);
system.debug('EXECTIME = ' + EXECTIME);
wp.add(w);
}
IF(wp.size()>=1){
update wp;
}
}
}
TEST CLASS
@istest
public class test_webProfileDailyUpdate {
public static WebProfileDailyUpdate wpdu = new WebProfileDailyUpdate();
public static datetime exec = wpdu.EXECTIME;
public static String CRON_EXP = '0 0 0 15 3 ? 2032';
static testmethod void test() {
system.debug('exec = ' + exec);
datetime dt = datetime.now().addDays(-1);
account a = new account();
testFactory.createSObject(a);
insert a;
web_profile__c twp = new web_profile__c(displayed_organization__c = a.id);
testFactory.createSObject(twp);
insert twp;
Test.startTest();
// this should have one record
list <web_profile__c> wps = new list<web_profile__c>([select id, temp_dummy__c from web_profile__c where temp_dummy__c = '']);
system.assert(wps.size() == 1);
// this should have one record
list <web_profile__c> wptest = new list<web_profile__c>([select Id, Temp_Dummy__c, Displayed_Organization__c FROM web_profile__c WHERE Displayed_Organization__r.LastModifiedDate >=: dt]);
system.assert(wps.size() == 1);
// Schedule the test job
String jobId = System.schedule('ScheduleWebProfileUpdateClassTest',
CRON_EXP,
wpdu);
// Get the information from the CronTrigger API object
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered,
NextFireTime
FROM CronTrigger WHERE id = :jobId];
// Verify the expressions are the same
System.assertEquals(CRON_EXP,
ct.CronExpression);
// Verify the job has not run
System.assertEquals(0, ct.TimesTriggered);
// Verify the next time the job will run
System.assertEquals('2032-03-15 00:00:00',
String.valueOf(ct.NextFireTime));
Test.stopTest();
list <web_profile__c> wpsu = new list<web_profile__c>([select id, temp_dummy__c from web_profile__c where temp_dummy__c != '']);
system.assert(wpsu.size() == 1);
system.assertEquals(wpsu[0].temp_dummy__c,string.valueOf(exec));
system.assertNotEquals(exec, NULL);
}
}