Friday, September 28, 2012

Testing An Expected Exception Message

To test that an exception is thrown in JUnit you can use @Test(expected = "WhateverException.class"), but the only drawback is that you can't test the exception's message.

A way to test the expected exception as well as the expected message is to use the @Rule annotation with ExpectedException.

Here is an example.

No comments:

Post a Comment