Is it possible to write tests where specific exception is expected, like using @Test(expected = UnhandledException::class) from JUnit ?
Thanks!
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.
Most helpful comment
One of Spek's transitive dependencies is
kotlin-testwhich hasassertFailsandassertFailsWith:See kotlin/kotlin/test/TestAssertions.kt and kotlin.jvm/kotlin/test/TestAssertionsJVM.kt.