inline fun <reified T : Throwable> shouldNotThrowExactly(block: () -> Any?): Unit
Verifies that a block of code doesn't throw a Throwable of type T, not including subclasses of T
Use this function to wrap a block of code that you'd like to verify whether it throws T (not including) or not. If T is thrown, this will thrown an AssertionError. If anything else is thrown, the throwable will be propagated. This is done so that no unexpected error is silently ignored.
This function won't include subclasses of T. For example, if you test for java.io.IOException and the code block throws java.io.FileNotFoundException, propagate the java.io.FileNotFoundException instead of wrapping it in an AssertionError.
If you wish to test T and subclasses, use shouldNotThrow.
If you don't care about the thrown exception, use shouldNotThrowAny
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 doesn't throw a Throwable, use shouldNotThrowExactlyUnit or it's variations.
val thrown: FooException = shouldThrowExactly<FooException> {
// Code that we expect to throw FooException
throw FooException()
}
See Also