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
Brian Nguyen 2Brian Nguyen 2 

Assertion failed during test

Hi,

I have wrote a trigger that sets a field call "Last Survey Sent" on the Account equal to the "Last Survey Sent" field on the Case. The field on the Case gets set via workflow when a survey is sent to a customer. As I am testing the apex class, I am getting the error: "System.AssertException: Assertion Failed: Expected: null, Actual: 2014-10-10 00:00:00
Class.UpdateSurvey.testLastSurveySent: line 21, column 1". Any ideas on how to resolve this issue?

Trigger:
Trigger UpdateLastSurveySent on Case (after insert, after update) {
    List<Account> accList = new List<Account>();
    for (Case c : Trigger.new) {
        Account acc = new Account(
            Id = c.Account.Id,
            Last_Survey_Sent__c = c.Last_Survey_Sent__c
        );
        accList.add(acc);
    }
    try {
        update accList;
    } catch (Exception ex) {
        System.debug('Could not update Last Survey Sent field on Account with cause: ' + ex.getCause());
    }


Apex Class:
@IsTest   
public class UpdateSurvey {
    @isTest static void testLastSurveySent() {
   
        // Insert Account and Case for tests
        // These may need additional fields, depending on your organization
        Account acc = new Account(
           Name = 'Test Account',
           Website = 'www.test.com',
           Phone = '8888888888'
        );
        insert acc;
        Case c = new Case(
            AccountId = acc.Id,
            Last_Survey_Sent__c = Date.Today()
        );
        insert c;
       
        // Test that the Last Survey Sent field on the account was updated on Insert
        Account accTest = [SELECT Id, Last_Survey_Sent__c FROM Account WHERE Id = :acc.id];
        System.assertEquals(accTest.Last_Survey_Sent__c, Date.Today());
       
        // Test that the Last Survey Sent field on the account changes on Update
        c.Last_Survey_Sent__c = Date.Today()+1;
        update c;
        accTest = [SELECT Id, Last_Survey_Sent__c FROM Account WHERE Id = :acc.id];
        System.assertEquals(accTest.Last_Survey_Sent__c, Date.Today());
    }
}

Vinay JVinay J
Dear Brian,

you are getting this error because of the execution oder. Triggers are executed before workflow. so when your trigger executes and checks last_survey_sent__c  == today, you get an error because last_survey_sent__c is null. Your workflow is not executed till now, but by writting  System.assertEquals(accTest.Last_Survey_Sent__c, Date.Today());  you are expecting it to be equal to today which will updated done after your trigger ends.. 

Resolution, I can think of as solution in case of update. when you update a record, update trigger will run, after that workflow will run and will update your record due to which, update trigger will again be executed. During the secon run of your trigger, this assert will pass. So, you should use asser only during second run of the trigger. You can use Static variable to check the second run.

For insert, I can't think of a way to check this.. probabily you'll have to update the record from insert trigger and then use the assert...