Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-5833

AssertJ methods setting the assertion context should come before an assertion

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Add an assertion predicate after calling this method.
    • Highlighting:
      Hide

      Method call identifier

      Show
      Method call identifier
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Java
    • Constant Cost:
      5min
    • Analysis Scope:
      Test Sources

      Description

      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

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                quentin.jaquier Quentin Jaquier
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: