inline fun <reified T : Throwable> shouldThrowExactlyUnit(block: () -> Unit): 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, when shouldThrowExactly can't be used for whatever reason, such as assignment operations (assignments are statements therefore has no return value).
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 shouldThrowUnit instead.
If you don't care about the thrown type at all, use shouldThrowAnyUnit instead.
val thrown: FooException = shouldThrowExactlyUnit<FooException> {
// Code that we expect to throw FooException
throw FooException()
}
See Also