Test class declares more than one @TestSetup method
ID |
apex.correctness.test_one_testsetup |
Severity |
high |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
medium |
Resource |
Test Quality |
Language |
Apex |
Rationale
A test class may have only one @TestSetup method. The platform does not reject the class at compile time, it simply runs one of them, so the records the other method was supposed to create never exist. Every case that relies on them then queries an empty table and fails on an index out of bounds or a null relationship, far from the setup method that was skipped. When the two setup methods create overlapping data the failure is worse still: some cases pass and some do not, depending on which method won, and the run looks flaky rather than wrong.
The following code illustrates the pattern detected by this rule:
// FLAGGED: Test class declares more than one @TestSetup method
@IsTest
private class OrderPipelineTest {
@TestSetup
static void createAccounts() {
insert new Account(Name = 'Acme');
}
@TestSetup
static void createOrders() {
Account a = [SELECT Id FROM Account];
insert new Order(AccountId = a.Id, Status = 'Draft');
}
@IsTest
static void ordersAreDraft() {
List<Order> orders = [SELECT Id, Status FROM Order];
Assert.areEqual(1, orders.size(), 'the setup order was expected');
}
}