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