jeudi 23 janvier 2020

Mockk: Verify method called within coroutine

I have a simple object which provides a suspend function to simulate a delaying network request and afterwards calls another method from the object.

class CoroutinesObject {

suspend fun doApiCall() {
    delay(1000)
    println("Hello from API")
    val apiResult = "result #1"

    callMe(apiResult)
}

fun callMe(result: String) {
    println("[${Thread.currentThread().name}] call me with result: $result")
}

}

I would like to write a simple test which should verify that the method callMe has been called.

class CoroutinesTest {

@Test
fun doApiCall_callsCallMe() {
    val obj = CoroutinesObject()
    runBlocking {
        obj.doApiCall()
    }

    coVerify { obj.callMe("result #1") }
}

}

Unfortunately the test fails with the following exception and I'm not sure why this happens.

io.mockk.MockKException: Missing calls inside verify { ... } block.

Anybody got an idea whats the problem and how to write a test which is able to verify the called method?

Aucun commentaire:

Enregistrer un commentaire