Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

12
  • 3
    IMO, this is the preferred method. Commented Jun 15, 2012 at 20:16
  • 15
    @LeviMorrison - IMHO the exception message should not be tested, similarly to log messages. Both are considered extraneous, helpful information when performing manual forensics. The key point to test is the type of exception. Anything beyond that is binding too tightly to the implementation. IncorrectPasswordException should be enough--that the message equals "Wrong password for [email protected]" is ancillary. Add to that that you want to spend as little time writing tests as possible, and you begin to see how important simple tests become. Commented Sep 5, 2013 at 5:26
  • 9
    @DavidHarkness I figured someone would bring that up. Similarly I would agree that testing messages in general is too strict and tight. However it is that strictness and tight binding that may (emphasized purposefully) be what is wanted in some situations, such as the enforcement of a spec. Commented Sep 6, 2013 at 5:40
  • 3
    I wouldn't watch in a doc-block to understand what it expected, but I'd look at the actual test code (regardless the kind of test). That's the standard for all the other tests; I don't see valid reasons for Exceptions to be (oh god) an exception to this convention. Commented Mar 27, 2015 at 9:52
  • 6
    The "don't test the message" rule sounds valid, unless you test a method which throws the same exception type in multiple parts of code, with the only difference being the error id, which is passed in the message. Your system might display a message to the user based on the Exception message (not the Exception type). In that case, it does matter which message the user sees, hence, you should test the error message.
    – Vanja D.
    Commented Apr 20, 2018 at 15:38