function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Fra6Fra6 

After trigger not running on test class

Hey all, I have an after trigger that runs fine and everything updates correctly. But I'm not sure why on my test class it's not running when I systemAssert at the end. 

public class OpportunityHelper {
    public static void updateDistributions(map<Id, Opportunity> newMap, map<Id, Opportunity> oldMap){
        for(Id opportunityID : newMap.keyset()){
            //get donations with their related distributions
            List<Opportunity> distributionsID = [
                SELECT (SELECT Id FROM DistributionDonation__r) 
                FROM Opportunity 
                WHERE Id = :opportunityID
            ];
            
            //check if donation date changed
            if(oldMap.get(opportunityID).CloseDate != newMap.get(opportunityID).CloseDate){
                Date newDate = newMap.get(opportunityID).CloseDate;
                //updated all related distributions
                for(Opportunity Opp : distributionsID) {
                    for(Product_Distribution__c distribution : Opp.DistributionDonation__r){
                        distribution.Date_TBD__c = newDate;
                    }
                    update Opp.DistributionDonation__r;
                }
            }
}
}

@isTest
public class OpportunityHelperTest {
    static testMethod void testDateChange() {

        Test.startTest();

        Product_Distribution__c insertedTest = TestHelper.createRecords();

        Date newDate = Date.newInstance(2001, 01, 01);
        
        Opportunity opp = [SELECT ID, CloseDate From Opportunity Where ID =: insertedTest.Product_Donation_Name__c];

        opp.CloseDate = newDate;

        Test.stopTest();

        Product_Distribution__c dis = [Select ID, Product_Donation_Name__c, Date_TBD__c From Product_Distribution__c Where ID =: insertedTest.Id];
        
        System.assertEquals(opp.CloseDate, dis.Date_TBD__c);
        
    }
}

It always ends up not updating and instead just gets the date that was initalized with. 


System.AssertException: Assertion Failed: Expected: 2001-01-01 00:00:00, Actual: 2019-05-13 00:00:00
Best Answer chosen by Fra6
Raj VakatiRaj Vakati
change you test class like below
 
@isTest
public class OpportunityHelperTest {
    static testMethod void testDateChange() {

        Test.startTest();

        Product_Distribution__c insertedTest = TestHelper.createRecords();

        Date newDate = System.today();
        
        Opportunity opp = [SELECT ID, CloseDate From Opportunity Where ID =: insertedTest.Product_Donation_Name__c];

        opp.CloseDate = newDate;

        Test.stopTest();

        Product_Distribution__c dis = [Select ID, Product_Donation_Name__c, Date_TBD__c From Product_Distribution__c Where ID =: insertedTest.Id];
        
        System.assertEquals(opp.CloseDate, dis.Date_TBD__c);
        
    }
}

 

All Answers

Maharajan CMaharajan C
Try to Update the opportunity after this line in test class

opp.CloseDate = newDate;
Update opp;

Thanks ,
Maharajan.C
Fra6Fra6
Thanks for replying Maharajan, I've updated it that way and reran the test and it still gives me the same result 
System.AssertException: Assertion Failed: Expected: 2001-01-01 00:00:00, Actual: 2019-05-13 00:00:00
 
Raj VakatiRaj Vakati
change you test class like below
 
@isTest
public class OpportunityHelperTest {
    static testMethod void testDateChange() {

        Test.startTest();

        Product_Distribution__c insertedTest = TestHelper.createRecords();

        Date newDate = System.today();
        
        Opportunity opp = [SELECT ID, CloseDate From Opportunity Where ID =: insertedTest.Product_Donation_Name__c];

        opp.CloseDate = newDate;

        Test.stopTest();

        Product_Distribution__c dis = [Select ID, Product_Donation_Name__c, Date_TBD__c From Product_Distribution__c Where ID =: insertedTest.Id];
        
        System.assertEquals(opp.CloseDate, dis.Date_TBD__c);
        
    }
}

 
This was selected as the best answer