kotest-assertions / io.kotest.assertions.throwables / shouldNotThrowUnit

shouldNotThrowUnit

inline fun <reified T : Throwable> shouldNotThrowUnit(block: () -> Unit): Unit

Verifies that a block of code doesn't 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 should be used when shouldNotThrow can't be used, such as when doing assignments (assignments are statements, therefore has no return value).

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 shouldNotThrowExactlyUnit instead.

If you don't care about the thrown exception type, use shouldNotThrowAnyUnit.

    shouldNotThrowUnit<FooException> {
       throw FooException() // Fails
    }

See Also

shouldNotThrow