Methods often throw exceptions but how do you test that your code throws the right exception? This example will show three different ways on how to validate or test for exceptions.
Expected NullPointerException
The first approach is to add the expected clause in the @Test method. The expected parameter is optional and if specified is a Throwable which cause the test method to succeed if an exception is caused within the method.
Expected IndexOutOfBoundsException
Exception w/ try/catch
The second approach is to use the fail assertion which will fail a test with a given exception.