I have a Salesforce test class with @TestSetup alongside two test methods. In the last method (testQueryDeletedRecord), I attempt to query a record that I deleted in a previous test method (testDeleteRecord). However, despite deleting the record, the query in testQueryDeletedRecord does not return null. Instead, it returns the deleted record. Can someone explain why this is happening? Here's my test class for reference:
@isTest
private class TestClassExample {
@TestSetup
static void setupTestData() {
// Create test records
Account acc = new Account(Name = 'Test Account');
insert acc;
}
@isTest
static void testDeleteRecord() {
// Retrieve the test Account created in setupTestData
Account testAcc = [SELECT Id, Name FROM Account WHERE Name = 'Test Account' LIMIT 1];
System.assertEquals('Test Account', testAcc.Name);
// Delete the test Account
delete testAcc;
}
@isTest
static void testQueryDeletedRecord() {
// Attempt to query the deleted Account
Account queriedAcc = [SELECT Id, Name FROM Account WHERE Name = 'Test Account' LIMIT 1];
// Assert that the queriedAcc is null since the record has been deleted
System.assert(queriedAcc == null, 'The result is not null');
}
}