question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing suspending function types fails when invocation is done in different context

See original GitHub issue

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'

Issue Analytics

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

github_iconTop GitHub Comments

6reactions
mtrewarthacommented, Sep 11, 2018

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!

5reactions
Ethan1983commented, Dec 19, 2018

It works with the new version of mockito-kotlin,

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found