Details
-
Type:
Code Smell Detection
-
Status: Active
-
Resolution: Unresolved
-
Labels:
-
Message:Refactor this method to reduce the number of assertions from {number of assertions} to less than {default value}.
-
Highlighting:
-
List of parameters:
-
Default Severity:Major
-
Impact:Low
-
Likelihood:High
-
Default Quality Profiles:Sonar way
-
Covered Languages:Java
-
Remediation Function:Constant/Issue
-
Constant Cost:20min
-
Analysis Scope:Test Sources
Description
A common good practice is to write test methods targeting only one logical concept, that can only fail for one reason.
While it might make sense to have more than one assertion to test one concept, having too many is a sign that a test became too complex and should be refactored to multiples ones.
This rule will report any test method containing more than a given number of assertion.
Noncompliant Code Example
With a parameter of 2.
@Test
void test() { // Refactor this method.
assertEquals(1, f(1));
assertEquals(2, f(2));
assertEquals(3, g(1));
}
Compliant Solution
void test_f() { assertEquals(1, f(1)); assertEquals(2, f(2)); } void test_g() { assertEquals(3, g(1)); }
Attachments
Issue Links
- is implemented by
-
SONARJAVA-3511 Rule S5961: Test methods should not contain too many assertions
-
- Closed
-
- is related to
-
SONARJAVA-3546 Issue message of S5961 should contains the number of actual assertions
-
- Closed
-