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

Test assertions should include messages

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Add a message to this assertion.
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Covered Languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Test Sources
    • Implementation details:
    • PMD:
      JUnitAssertionsShouldIncludeMessage

      Description

      Adding messages to JUnit, FEST and AssertJ assertions is an investment in your future productivity. Spend a few seconds writing them now, and you'll save a lot of time on the other end when either the tests fail and you need to quickly diagnose the problem, or when you need to maintain the tests and the assertion messages work as a sort of documentation.

      Noncompliant Code Example

      assertEquals(4, list.size());  // Noncompliant
      
      try {
        fail();  // Noncompliant
      } catch (Exception e) {
        assertThat(list.get(0)).isEqualTo("pear");  // Noncompliant
      }
      

      Compliant Solution

      assertEquals("There should have been 4 Fruits in the list", 4, list.size());
      
      try {
        fail("And exception is expected here");
      } catch (Exception e) {
        assertThat(list.get(0)).as("check first element").overridingErrorMessage("The first element should be a pear, not a %s", list.get(0)).isEqualTo("pear"); 
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                ann.campbell.2 Ann Campbell
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated: