Posts

Showing posts from September, 2009

Exceptions in JUnit Tests

When an exception is thrown in a unit test, the unit test will fail, giving a red bar - unless there is a try/catch that catches the exception and swallows it quietly.  Therefore, you should make sure that catching an exception in a unit test is indeed what you really mean to do.  Should the test really handle the error and continue, perhaps succeeding, or should the test fail, giving a visual indication that something went wrong? I'd add that when the right behavior of a test case is a thrown Exception, one should add to the @TEST annotation one's expectation.  e.g. @Test(expected = NumberFormatException.class)