I can write an assertion message one of two ways. Stating success:
assertEquals( "objects should be identical", expected, actual );
Or stating the condition of being broken:
assertEquals( "objects aren't identical", expected, actual );
Is there a standard for this in JUnit specifically? If not, what are the arguments for each side?
P.S. I've seen articles on the web demonstrating both of these without explanation, so just saying "search Google" is not an answer!
[UPDATE]
Everyone is getting hung up on the fact that I used assertEquals
and therefore the message is probably useless. But of course that's just because I wanted to illustrate the question simply.
So imagine instead it's:
assertTrue( ... big long multi-line expression ... );
Where a message is useful.
When a JUnit test is "Failed", u have gotten an AssertionException. Means in your case result was false, where it should have been true.
The fail assertion fails a test throwing an AssertionError. It can be used to verify that an actual exception is thrown or when we want to make a test failing during its development. In JUnit 5 all JUnit 4 assertion methods are moved to org.
I rarely even bother with a message, at least for assertEquals
. Any sensible test runner will explain that you were using assertEquals
and the two things which were meant to be equal. Neither of your messages give more information than that.
I usually find that unit test failures are transient things - I'll rapidly find out what's wrong and fix it. The "finding out what's wrong" usually involves enough detail that a single message isn't going to make much difference. Consider "time saved by having a message" vs "time spent thinking of messages" :)
EDIT: Okay, one case where I might use a message: when there's a compact description in text which isn't obvious from the string representation of the object.
For example: "Expected date to be December 1st" when comparing dates stored as milliseconds.
I wouldn't worry about how you express it exactly though: just make sure it's obvious from the message which way you mean. Either "should be" or "wasn't" is fine - just "December 1st" wouldn't be obvious.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With