While refactoring a test to make it more consistent with the style of our other tests, a colleague came upon something really weird.
The original test created the test account in @TestSetup, like so:
@IsTest
public TestClass2 {
private static final Boolean doInsert = true;
@TestSetup
public static void beforeTest() {
TestAccountFactory.create(doInsert);
}
@IsTest
public static void testSomething() {
User testUser = TestUserFactory.create(doInsert);
Account testAccount = [SELECT Id FROM Account LIMIT 1];
String expectedStatus = 'Active';
System.runAs(testUser) {
Test.startTest();
SpagettiCode.abandonAllHopeAllYeWhoEnterHere();
// Passes
System.assertEquals(
expectedStatus,
[SELECT Status__c FROM Account
WHERE Id = :testAccount.Id].Status__c]
);
Test.stopTest();
}
// Passes
System.assertEquals(
expectedStatus,
[SELECT Status__c FROM Account
WHERE Id = :testAccount.Id].Status__c]
);
}
}
As noted, the assertions pass.
In the modified form, the test created the Account within the test, like so:
@IsTest
public TestClass1 {
private static final Boolean doInsert = true;
@IsTest
public static void testSomething() {
User testUser = TestUserFactory.create(doInsert);
Account testAccount = TestAccountFactory.create(doInsert);
String expectedStatus = 'Active';
System.runAs(testUser) {
Test.startTest();
SpagettiCode.abandonAllHopeAllYeWhoEnterHere();
// Passes
System.assertEquals(
expectedStatus,
[SELECT Status__c FROM Account
WHERE Id = :testAccount.Id].Status__c]
);
Test.stopTest();
}
// Fails
System.assertEquals(
expectedStatus,
[SELECT Status__c FROM Account
WHERE Id = :testAccount.Id].Status__c]
);
}
}
Now, the second assertion fails even though the only difference is that the Account was not created in @TestSetup.
This is obviously a simplification of the code and our path to discovering this discrepancy was a torturous one, but we are trying to understand how this difference would even be possible and whether this is something that we could have expected or reflects a defect in how SFDC is executing the test?
doInsert
out of scope.