inline fun <reified T : Throwable> shouldThrow(block: () -> Any?): T
Verifies if a block of code will 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.
This function will include subclasses of T. For example, if you test for java.io.IOException and the code block throws java.io.FileNotFoundException, the test will pass.
If you wish to test for a specific class strictly (excluding subclasses), use shouldThrowExactly instead.
If you don't care about the thrown exception, use shouldThrowAny.
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 shouldThrowUnit or it's variations.
val thrownException: FooException = shouldThrow<FooException> {
// Code that we expect to throw FooException
throw FooException()
}
See Also