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