March 2015
Intermediate to advanced
236 pages
5h 26m
English
In addition to ensuring that the happy path through our code works, we want to verify that exceptions get thrown when expected. Understanding the conditions that cause a class to throw exceptions can make life a lot easier for a client developer using the class.
JUnit supports at least three different ways of specifying that you expect an exception to be thrown. Let’s examine a simple case: ensure that Account code throws an Exception when a client attempts to withdraw more than the available balance.
The JUnit @Test annotation supports passing an argument that specifies the type of an expected exception:
| iloveyouboss/13/test/scratch/AssertTest.java | |
| | @Test(expected=InsufficientFundsException.class) ... |
Read now
Unlock full access