Exceptions
To assert that a given block of code throws an exception, one can use the shouldThrow
function. Eg,
You can also check the caught exception:
If you want to test that a specific type of exception is thrown, then use shouldThrowExactly<E>
. For example, the
following block would catch a FileNotFoundException
but not a IOException
even though FileNotFoundException
extends from IOException
.
If you simply want to test that any exception is thrown, regardles of type, then use shouldThrowAny
.