Reputation: 121
I wrote an apex trigger to update a field (status_update_date) when an account's status is changed.
In the sandbox, if I change an account's status, the status_update_date successfully updates.
When I run my test class, the trigger does not get called and I get a null value for the status_update_date. Any ideas why the trigger is not called? Thanks!
Code below with trigger commented out at bottom of code:
@isTest
private class UpdateStatusTest{
public static Account A;
public static String initStatus = '';
public static String finalStatus = 'Fully Active';
static testMethod void testWithAccount() {
A = new Account(
Status__c = initStatus,
Name = 'TestName'
);
insert A;
test.StartTest();
A.Status__c = finalStatus;
update A;
test.StopTest();
// System.assertEquals(finalStatus, A.Status__c);
System.assertEquals(system.today(),A.Status_Update_Date__c);
}
}
// trigger UpdateStatus on Account (before update) {
//for(Account a: trigger.new){
//If the status has changed, update the date
// if (trigger.oldMap.get(a.Id).Status__c != trigger.newMap.get(a.Id).Status__c) {
// a.Status_Update_Date__c = system.today();
// }
//}
//}
Upvotes: 2
Views: 2384
Reputation: 56
After update you should get this record from database by soql
a=[SELECT Id, Status__c FROM Account WHERE Id=:a.Id];
System.assertEquals(finalStatus, A.Status__c);
Upvotes: 3