-
Type:
Bug Detection
-
Status: Active
-
Resolution: Unresolved
-
Message:Add an assertion predicate after calling this method.
-
Highlighting:
-
Default Severity:Major
-
Impact:Low
-
Likelihood:High
-
Default Quality Profiles:Sonar way
-
Covered Languages:Java
-
Constant Cost:5min
-
Analysis Scope:Test Sources
Describing, setting error message or adding a comparator in AssertJ must be done before calling the assertion, otherwise, settings will not be taken into account.
This rule raises an issue when one of the method (with all similar methods):
- as
- describedAs
- withFailMessage
- overridingErrorMessage
- usingComparator
- usingElementComparator
- extracting
- filteredOn
is called without calling an AssertJ assertion afterward.
Noncompliant Code Example
assertThat(actual).isEqualTo(expected).as("Description"); // Noncompliant assertThat(actual).isEqualTo(expected).withFailMessage("Fail message"); // Noncompliant assertThat(actual).isEqualTo(expected).usingComparator(new CustomComparator()); // Noncompliant
Compliant Solution
assertThat(actual).as("Description").isEqualTo(expected); assertThat(actual).withFailMessage("Fail message").isEqualTo(expected); assertThat(actual).usingComparator(new CustomComparator()).isEqualTo(expected);
See
- is implemented by
-
SONARJAVA-3390 Rule S5833: AssertJ methods setting the assertion context should come before an assertion
-
- Closed
-