I have a custom object with a formula field that references a couple other fields on the object. The class I've written my test class for requires the formula field, which it uses in a SOQL query. Problem is, rather than populating, the formula field remains null, causing the test to fail.
This issue has come up before, and the accepted solution on other posts is to simply query the formula field on the test record before triggering your actual code. But I've tried this and it doesn't work!
Here's the relevant code from my test class ('Time_Period_c' is the formula date field that uses 'Month_c' and 'Year__c'):
Sales_vs_Budget_Reporting__c sRec1 = new Sales_vs_Budget_Reporting__c(Business_Unit__c = 'IP', Month__c = 'March', Year__c = '2014', Product_Group__c = 'Public Workshop', Record_Type__c = 'Closed Won', Salesperson__c = '00580000001rWsQ'); insert sRec1; Sales_vs_Budget_Reporting__c pop1 = [SELECT ID, Time_Period__c FROM Sales_vs_Budget_Reporting__c Where ID =:sRec1.ID]; system.debug(sRec1.Time_Period__c);
Then I run the starttest which calls my class (its a scheduled class not a trigger), which also happens to query the formula field and the records. But that system.debug call returns NULL.
What's the deal?