Testing exceptions
We have mentioned this before in Chapter 1, Getting Started with Testing, where we stated that you should test for exceptions and wrong values instead of just testing positive cases:
@Test(expected = InvalidTemperatureException.class) public final void testExceptionForLessThanAbsoluteZeroF() { TemperatureConverter. fahrenheitToCelsius(TemperatureConverter.ABSOLUTE_ZERO_F - 1); } @Test(expected = InvalidTemperatureException.class) public final void testExceptionForLessThanAbsoluteZeroC() { TemperatureConverter. celsiusToFahrenheit(TemperatureConverter.ABSOLUTE_ZERO_C - 1); }
We have also presented these tests before, but here, we are digging deeper into it. The first thing to notice is that these are JUnit4 tests, meaning we can test for exceptions using the expected
annotation parameter. When you download the chapter's sample project, you will be able to see that it is split into two modules, one of them being core, which is a pure Java module, and so, we have...