File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Testing and the fly likes do you use message field on assertions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "do you use message field on assertions?" Watch "do you use message field on assertions?" New topic

do you use message field on assertions?

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33130

At Lasse's Server Side breakout session, I noticed he didn't have the String message parameter in any of the examples. When I asked, Lasse said he feels it makes the tests harder to read or doesn't add clarity or something like that.

The following are the first unit tests I've written since that discussion. I'm not clear how the message doesn't help.
If the tests fails, it says "java.lang.AssertionError: #attachments expected: <1> but was: <3>" instead of "java.lang.AssertionError: expected: <1> but was: <3>"
Here's a few reasons:
1) The failure message is more descriptive and tells me the magic numbers 1 and 3 pertain to the number of attachments.
1a) I don't need to click on the failure to see why it is failing
1b) If I have multiple assertions, I know which one is failing
1c) If the nightly build fails, I can see why without having to go to the code by looking up the line number
2) Writing a message string encourages the code author to think about what the assertion is testing.

I'm hoping Lasse or someone else with the same opinion reads this so I can better understand the reasoning. Or just know that it depends on circumstances. In a true TDD environment, I'm guessing some of these don't come up. (or aren't an issue since you wrote the offending code a minute ago)

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: do you use message field on assertions?
It's not a secret anymore!