mockito-kotlin: Testing suspending function types fails when invocation is done in different context

Run this test code:

val suspendFunction : suspend () -> Unit = mock()

runBlocking {
    suspendFunction.invoke()
}

runBlocking {
    verify(suspendFunction).invoke()
}

It should succeed (function was invoked, so verify should pass), but it fails with an error:

Argument(s) are different! Wanted:
function1.invoke(
    (testLambda$2) Function2<kotlinx.coroutines.experimental.CoroutineScope, kotlin.coroutines.experimental.Continuation<? super kotlin.Unit>, java.lang.Object>
);
-> at LambdaTest$testLambda$2.doResume(TestTest.kt:19)
Actual invocation has different arguments:
function1.invoke(
    (testLambda$1) Function2<kotlinx.coroutines.experimental.CoroutineScope, kotlin.coroutines.experimental.Continuation<? super kotlin.Unit>, java.lang.Object>
);

Reproduced on versions:

ext.kotlin_version = '1.2.31'
compile "org.jetbrains.kotlinx:kotlinx-coroutines-android:0.22.5"
testCompile 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0-alpha03'

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Comments: 21 (5 by maintainers)

Commits related to this issue

Most upvoted comments

We can debate what to call this (“bug” or “not a bug”) all we want, but it doesn’t really matter. The important thing is that we’re looking for a way to mock and verify suspend functions. Given that suspend functions are a Kotlin concept, and we’re trying to mock/verify them, a Mockito concept, it seems rather fitting that mockito-kotlin provide a way to do this.

EDIT: If there’s a way to do this that I’m just not aware of, I’m all ears!

It works with the new version of mockito-kotlin,

testCompile ‘com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0’