inline fun <reified T : Throwable> shouldThrowExactly(block: () -> Any?): T
Verifies that a block of code throws a Throwable of type T, not including subclasses of T
Use this function to wrap a block of code to verify if it throws a specific throwable T
This function will exclude subclasses of T. For example, if you test for java.io.IOException and the code block throws java.io.FileNotFoundException, the test will fail, as java.io.FileNotFoundException is a subclass of java.io.IOException, but not exactly java.io.IOException.
If you wish to include any subclasses, you should use shouldThrow instead.
If you don't care about the thrown type at all, use shouldThrowAny instead.
Attention to assignment operations:
When doing an assignment to a variable, the code won't compile, because an assignment is not of type Any, as required by block. If you need to test that an assignment throws a Throwable, use shouldThrowExactlyUnit or it's variations.
val thrown: FooException = shouldThrowExactly<FooException> {
// Code that we expect to throw FooException
throw FooException()
}
See Also