kotest-assertions / io.kotest.assertions.throwables / shouldThrowUnit

shouldThrowUnit

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

Verifies if a block of code throws 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 should be used when shouldThrow 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, the test will pass.

If you wish to test for a specific class strictly (excluding subclasses), use shouldThrowExactlyUnit instead.

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

    val thrownException: FooException = shouldThrowUnit<FooException> {
        // Code that we expect to throw FooException
        throw FooException()
    }

See Also

shouldThrow