Skip to content

Suiteopensavvy.prepared.suite.assertionscheckThrows

checkThrows

inline fun <T : Throwable> checkThrows(block: () -> Any?): T

Verifies that block throws an exception of type T.

If block indeed throws an exception of type T, it is returned. In all other cases, an AssertionError is thrown.

Example

checkThrows<IllegalStateException> {
    yourService()
}

If the expected exception is thrown, it is returned by the function, so you can write further assertions. For example, if you want an exception with a specific message, write:

val e = checkThrows<IllegalStateException> {
    yourService()
}
check(e.message = "Cannot create a new user because another one already exists")