Testing suspending function types fails when invocation is done in different context
See original GitHub issueRun 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'
Issue Analytics
- State:
- Created 5 years ago
- Comments:21 (5 by maintainers)
Top Results From Across the Web
Testing Kotlin coroutines on Android
Wrapping your test's code in runTest will work for testing basic suspending functions, and it will automatically skip any delays in coroutines, making...
Read more >Unit testing a Kotlin coroutine with delay - Stack Overflow
TestCoroutineDispatcher, TestCoroutineScope, or Delay can be used to handle a delay in a Kotlin coroutine made in the production code tested.
Read more >Testing Kotlin Coroutines - Kt. Academy
Testing suspending functions in most cases is not different from testing normal functions. Take a look at the fetchUserData below from FetchUserUseCase ....
Read more >Mocking and stubbing suspend functions with MockK
How to easily stub or mock suspend function in Kotlin? MockK is the solution. See MockK examples for coroutines testing.
Read more >Composing suspending functions - Kotlin
Consider what happens if between the val one = somethingUsefulOneAsync() line and one.await() expression there is some logic error in the code, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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’