Testing patterns
Testing exceptional flow is a bit trickier than verifying the outcome of normal execution paths. The following section will explain why and introduce the different techniques available to get this job done.
Using the fail statement
"Always expect the unexpected" | ||
--Adage based on Heraclitus |
Testing corner cases often results in the necessity to verify that a functionality throws a particular exception. Think, for example, of a java.util.List
implementation. It quits the retrieval attempt of a list's element by means of a non-existing index number with java.lang.ArrayIndexOutOfBoundsException
.
Working with exceptional flow is somewhat special as without any precautions, the exercise phase would terminate immediately. But this is not what we want since it eventuates in a test failure. Indeed, the exception itself is the expected outcome of the behavior we want to check.
From this, it follows that we have to capture the exception before we can verify...