inline fun <reified T : Throwable> shouldNotThrow(block: () -> Any?): Unit
Verifies that a block of code will not throw a Throwable of type T or subtypes
Use this function to wrap a block of code that you'd like to verify whether it throws T (or subclasses) 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 will include all subclasses of T. For example, if you test for java.io.IOException and the code block throws java.io.FileNotFoundException, this will also throw an AssertionError instead of propagating the java.io.FileNotFoundException directly.
If you wish to test for a specific class strictly (excluding subclasses), use shouldNotThrowExactly instead.
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 shouldNotThrowUnit or it's variations.
val thrownException: FooException = shouldThrow<FooException> {
throw FooException() // Fails
}
See Also