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

Description

Reports a test class declaring two or more @TestSetup methods.

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');
    }
}

Remediation

Merge the bodies into a single @TestSetup method, in the order the data depends on, and let the remaining helpers be plain static methods that the individual cases call. Where the cases genuinely need different fixtures, split the class so each test class has its own single @TestSetup.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved