2

I am learning Kotlin and MockK. I have seen how to verify that a method was called and how to check the result. But how would I check, that the method has thrown an exception?

DanielBK
  • 892
  • 8
  • 23

2 Answers2

3

Sorry, I found it: assertFailsWith<EceptionClass> { methodCall() }

DanielBK
  • 892
  • 8
  • 23
-2

This is my approach, hope its helping you

class TestClass {
    fun testVerify() {
        throw Exception("exception")
    }
}
@Test
fun mockTest() {
    try {
        TestClass().testVerify()
    }
    catch (e: Exception) {
        assertEquals(e.message, "exception")
    }
}
Đàm Tùng
  • 329
  • 1
  • 3
  • 7