Dr. Dobb's Journal March, 2006
Tip #7: Controlled Failures Need Exceptions
Sometimes exceptions need to be used as a way to control program flow. This is true when writing unit tests, too. Here, the execution of the code to test is expected to produce a failure. It would be problematic if a failure does not occur. By asserting "not null" on the exception in the finally clause (as in the following example), the unit test passes:
public void testExceptionIsThrown {
Exception exception;
try{
// something that generates an exception
...
} catch( Exception x ) {
exception = x;
} finally {
assertNotNull(ÒException was thrownÓ, exception);
}
}
Benjamin Booth
http://www.benjaminbooth.com/