rspec/rules/S5833/java/rule.adoc
2022-02-04 16:28:24 +00:00

53 lines
1.4 KiB
Plaintext

Describing, setting error message or adding a comparator in https://assertj.github.io/doc/[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
[source,java]
----
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
[source,java]
----
assertThat(actual).as("Description").isEqualTo(expected);
assertThat(actual).withFailMessage("Fail message").isEqualTo(expected);
assertThat(actual).usingComparator(new CustomComparator()).isEqualTo(expected);
----
== See
* https://assertj.github.io/doc/#calling-as-after-the-assertion[AssertJ incorrect usage documentation]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::highlighting.adoc[]
endif::env-github,rspecator-view[]