mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.11k stars 202 forks source link

Verification of suspend functions? #431

Closed alwa closed 3 years ago

alwa commented 3 years ago

I've read the wiki and there is no example how to verify that a suspend function has been called.

Is this implemented?

In Mockk the equivalent is called "coVerify()", see: https://mockk.io/

bohsen commented 3 years ago

It's verify or verifyBlockingwith mockito-kotlin:

@Test
fun verifySuspendFunctionCalled() {
    runBlocking {
        /* Given */
        val m = mock<SomeInterface>()

        /* When */
        m.suspending() }

        /* Then */
        verify(m).suspending()
    }
}

...

@Test
fun verifySuspendFunctionCalled_verifyBlocking() {
    val m = mock<SomeInterface>()

    runBlocking { m.suspending() }

    verifyBlocking(m) { suspending() }
}

Take a look at the tests.

TimvdLippe commented 3 years ago

Closing per the above. Thanks for the explanation @bohsen !