A developer wrote a trigger on Opportunity that will update a custom Last Sold Date field on the Opportunity's Account whenever an Opportunity is closed. In the test
class for the trigger, the assertion to validate the Last Sold Date field fails.
What might be causing the failed assertion?
A. The test class has not defined an Account owner when inserting the test data.
B. The test class has not implemented seealldata=true in the test method.
C. The test class has not re-queried the Account record after updating the Opportunity.
D. The test class is not using System. runs () to run tests as a Salesforce administrator.
C. The test class has not re-queried the Account record after updating the Opportunity.
Explanation:
To understand why the assertion fails in this trigger test scenario, we need to focus on how data is handled in Apex tests, especially regarding trigger behavior and record state in memory vs. the database.
✅ Correct Answer: C. The test class has not re-queried the Account record after updating the Opportunity.
✅ Explanation:
When you update a record in Apex (like an Opportunity), any related changes made by triggers (like updating a field on the parent Account) occur in the database. However, the in-memory version of the Account (from earlier in the test method) does not automatically reflect those changes.
So, if your trigger updates the Last_Sold_Date__c field on Account when an Opportunity is closed, but the test class does not perform a fresh SOQL query to retrieve the updated Account, then any assertion against that field will still reflect the old value (likely null) — causing the test to fail.
✅ Proper fix in the test class:
Account acct = [SELECT Id, Last_Sold_Date__c FROM Account WHERE Id = :accountId];
System.assertEquals(expectedDate, acct.Last_Sold_Date__c);
❌ A. The test class has not defined an Account owner when inserting the test data.
This is not necessary unless your trigger or validation rules explicitly depend on OwnerId. Salesforce assigns a default owner (usually the running user) if none is provided. So this won’t affect the Last_Sold_Date__c update unless additional logic is involved (not mentioned here).
❌ B. The test class has not implemented seeAllData=true in the test method.
Using seeAllData=true is discouraged and unnecessary here. Since you're inserting test Account and Opportunity records within the test method, no real org data is required. The test should work with only mock/test data.
❌ D. The test class is not using System.runAs() to run tests as a Salesforce administrator.
System.runAs() is used to test behavior under different user roles or profiles (e.g., field-level security or sharing rules). It is not required for triggers to function unless access issues are part of the logic. Again, this isn’t relevant in the scenario described.
✅ Final Verdict:
C. The test class has not re-queried the Account record after updating the Opportunity is the correct answer because without re-querying, the test accesses stale data, leading to a failed assertion.