Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions rules/S8037/apex/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"title": "Test classes should have only one @testSetup method",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant/Issue",
"constantCost": "5 min"
},
"tags": [
"apex",
"test"
],
"defaultSeverity": "Blocker",
"ruleSpecification": "RSPEC-8037",
"sqKey": "S8037",
"scope": "Tests",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "unknown",
"code": {
"impacts": {
"RELIABILITY": "BLOCKER",
"MAINTAINABILITY": "BLOCKER"
},
"attribute": "LOGICAL"
}
}
71 changes: 71 additions & 0 deletions rules/S8037/apex/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
This is an issue when a test class contains multiple methods annotated with `@testSetup`.

== Why is this an issue?

In Apex, test classes can only contain one `@testSetup` method. The `@testSetup` annotation is used to create test data that will be available to all test methods in the class, improving test performance by avoiding duplicate data creation.

When you define multiple `@testSetup` methods in the same test class, the Apex compiler will reject the code and throw a compilation error. This happens because the Salesforce platform expects exactly one setup method per test class to maintain predictable test execution order and data consistency.

The `@testSetup` method runs once before all test methods in the class, creating a clean, consistent data state. Having multiple setup methods would create ambiguity about execution order and could lead to unpredictable test behavior.

=== What is the potential impact?

Multiple `@testSetup` methods will cause compilation errors, preventing the test class from being deployed or executed. This blocks development progress and can prevent deployments to production environments.

== How to fix it

Consolidate all setup logic into a single @testSetup method. Move the code from multiple setup methods into one method and remove the duplicate @testSetup annotations.

=== Code examples

==== Noncompliant code example

[source,apex,diff-id=1,diff-type=noncompliant]
----
@isTest
public class MyTestClass {
@testSetup
static void setup1() {
// setup code
Account acc = new Account(Name = 'Test Account');
insert acc;
}

@testSetup // Noncompliant
static void setup2() {
// more setup code
Contact con = new Contact(LastName = 'Test Contact');
insert con;
}
}
----

==== Compliant solution

[source,apex,diff-id=1,diff-type=compliant]
----
@isTest
public class MyTestClass {
@testSetup
static void setup() {
// all setup code in one method
Account acc = new Account(Name = 'Test Account');
insert acc;

Contact con = new Contact(LastName = 'Test Contact');
insert con;
}
}
----

== Resources

=== Documentation

* Apex Testing Framework - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_framework.htm[Official Salesforce documentation on Apex testing framework and @testSetup usage]

* Unit Testing in Apex - https://www.apexhours.com/unit-testing-in-apex[Comprehensive guide on Apex unit testing including @testSetup best practices]

=== Related rules

* RSPEC-3411 - https://rules.sonarsource.com/csharp/RSPEC-3411/[Similar rule for C# test classes with multiple setup methods]
2 changes: 2 additions & 0 deletions rules/S8037/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Loading