0

Context: I have a child object Child__c that has a parent Parent__c object. The Parent__c object has a Roll Up Summary field that displays the Minimum Created Date of all the Child__c records associated to the Parent__c record.

testParent record is created in a testSetup method, not shown in the example.

@isTest 
static void testRollUp(){

        Parent__c testParent = [SELECT Id FROM Parent__c LIMIT 1];

        Child__c testChild = new Child__c (
            Name = 'testChild',
            Parent__c = testParent.Id
        );

        insert testChild;
        // Up to this point, the Roll-Up Summary field Roll_Up_With_Min_Date_Child__c in Parent__c should be populated with the Minimum Created Date of the Child__c record.
        // Set the Created Date using the method in the Test Class, minus 5 days.
        test.setCreatedDate(testChild.Id, Datetime.Now().addDays(-5));
        test.startTest();
// Retrieving the Parent__c record, Roll_Up_With_Min_Date_Child__c shows the value of the current date-time, not date-time minus 5 days. 
        testParent = [SELECT Id, 
                            Roll_Up_With_Min_Date_Child__c
                            FROM Parent__c LIMIT 1];
        // Therefore this assertion fails.
        System.assertEquals(Datetime.Now().addDays(-5),testParent.Roll_Up_With_Min_Date_Child__c);
        test.stopTest();
}

The problem is that my assertion fails because the value in testParent.Roll_Up_With_Min_Date_Child__c is datetime.now(), not datetime.now.addDays(-5).

How can I mock the roll-up summary? Am I retrieving the value incorrectly?

6
  • 4
    the RSF executes immediately upon execution of insert testChild. It is not clear to me why you need to test OOTB SFDC behavior
    – cropredy
    May 25, 2023 at 17:55
  • Agreed that it is inadvisable to write unit tests against out-of-the-box features.
    – Adrian Larson
    May 25, 2023 at 19:52
  • Thank you @Cropredy and adrian-larson. The reason why I need to test it like that is not relevant to my problem. The logic is far more complex and I simply summarized the case using the assertion. There is a method to which I pass the Parent__c record, that evaluates if testParent.Roll_Up_With_Min_Date_Child__c is greater than Datetime.today(). Since testParent.Roll_Up_With_Min_Date_Child__c is always being equals to Datetime.Today() (because the records are created at the same time), the logic that complies with the condition is not being evaluated programmatically using unit tests. May 25, 2023 at 21:39
  • 1
    see salesforce.stackexchange.com/a/61246/2602 for a way to dependency inject dates and datetimes into your tests;
    – cropredy
    May 26, 2023 at 0:33
  • Hi, @cropredy, yes, that worked for me. Thank you. May 29, 2023 at 16:43

1 Answer 1

0

I couldn't change the CreatedDate in a child object to use that value in a Roll up summary field in a Parent object, but thanks to @cropredy, I was able to use a Workaround: Salesforce: Is there any way to set System time in test class

Thanks, everybody.

Not the answer you're looking for? Browse other questions tagged .