Spek: Handle expected exception

Created on 6 Jun 2016  路  3Comments  路  Source: spekframework/spek

Is it possible to write tests where specific exception is expected, like using @Test(expected = UnhandledException::class) from JUnit ?
Thanks!

Most helpful comment

One of Spek's transitive dependencies is kotlin-test which has assertFails and assertFailsWith:

assertFailsWith<IllegalStateException> { 
    check(false)
}

See kotlin/kotlin/test/TestAssertions.kt and kotlin.jvm/kotlin/test/TestAssertionsJVM.kt.

All 3 comments

For now you definitely can use plain try/catch check inside of it {}

If you use https://github.com/npryce/hamkrest you can do:

assertThat({throw ExampleException("testing")}, throws<ExampleException>())
// negation
assertThat({ /* do something */ }, !throws<Throwable>())

One of Spek's transitive dependencies is kotlin-test which has assertFails and assertFailsWith:

assertFailsWith<IllegalStateException> { 
    check(false)
}

See kotlin/kotlin/test/TestAssertions.kt and kotlin.jvm/kotlin/test/TestAssertionsJVM.kt.

Was this page helpful?
0 / 5 - 0 ratings