I am trying to test some code that is working in the sandbox. The code, shown below, uses a reference to obtain the BusinessHoursId field of the Milestone's Case object (m.Case__r.BusinessHoursId):
public static Integer getNumMinutesTaken(Milestone__c m) {
Long diffInMillis = BusinessHours.diff(m.Case__r.BusinessHoursId, m.Start__c, m.CompletionDatetime__c);
return (Integer)diffInMillis / (1000 * 60);
}
This works fine in the sandbox, as I have said, but for some reason I cannot use the same type of reference in my test code to obtain the BusinesshoursId:
// Test Code Snippet
Case c = getValidCase();
c.BusinessHoursId = defaultBH.Id;
insert c;
System.debug('c.Id'+c.Id); // valid id
System.debug('c.BusinessHoursId'+c.BusinessHoursId); // valid id
Datetime start = datetime.newInstance(2012, 10, 5, 16, 0, 0);
Datetime completed = datetime.newInstance(2012, 10, 5, 17, 0, 0);
Milestone__c ms = new Milestone__c(Case__c = c.Id, Start__c = start, CompletionDatetime__c = completed);
System.debug(ms.Case__r.BusinessHoursId); // null
Integer timeTaken = MilestoneUtils.getNumMinutesTaken(ms);
System.assertEquals(60, timeTaken);
As far as I can see, this is the same reference as in my method, but it is returning null. Is there a difference to the allowable ways of referencing things in test code as compared to live code?
