0

What is the recommended style of the "message" in a JUnit test?

Here is the message expressed currently:

ReturnedObject ro = objectUnderTest.methodToTest(stub);
Assert.assertNotNull("The object was null!", ro);

Or, is this more correct?

ReturnedObject ro = objectUnderTest.methodToTest(stub);
Assert.assertNotNull("Verify that the object was not null.", ro);

Please pay attention, as you answer my question, to the appearance of the message from the point of view of the user.

djangofan
  • 28,471
  • 61
  • 196
  • 289

1 Answers1

4

You should be able to read exactly what failed directly from the failure message. Which conveys the most useful information?

  1. java.lang.AssertionError: The object was null!
  2. java.lang.AssertionError: Verify that the object was not null.
  3. java.lang.AssertionError: ObjectUnderTest.methodToTest returned null.

Beyond that, it's just a matter of team style.

Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251