kotest-assertions / io.kotest.assertions.throwables / shouldThrowAny

shouldThrowAny

inline fun shouldThrowAny(block: () -> Any?): Throwable

Verifies that a block of code throws any Throwable

Use function to wrap a block of code that you want to verify that throws any kind of Throwable.

If you want to verify a specific Throwable, use shouldThrowExactly.

If you want to verify a Throwable and any subclasses, use shouldThrow

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 shouldThrowAnyUnit or it's variations.

    val thrownThrowable: Throwable = shouldThrowAny {
        throw FooException() // This is a random Throwable, could be anything
    }

See Also

shouldThrowAnyUnit