Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should the JUnit message state the condition of success or failure?

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.

like image 947
Jason Cohen Avatar asked Jul 02 '09 15:07

Jason Cohen


People also ask

When a JUnit test fails What does it mean?

When a JUnit test is "Failed", u have gotten an AssertionException. Means in your case result was false, where it should have been true.

What happens when JUnit assertion fails?

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.


1 Answers

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.

like image 62
Jon Skeet Avatar answered Oct 08 '22 02:10

Jon Skeet