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.

Coroutine/suspend function mocking support

See original GitHub issue

If you use non-inline lambdas that are not declared as suspend, the runBlocking { } around the test function doesn’t help, rather we need to mock { on { runBlocking { suspendFun() } } } for each mock…

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:19

github_iconTop GitHub Comments

15reactions
nhaarmancommented, Dec 3, 2017

I don’t really use coroutines much myself. I can suggest this solution:

fun <T : Any, R> KStubbing<T>.onBlocking(
      m: suspend T.() -> R
): OngoingStubbing<R> {
    return runBlocking { Mockito.`when`(mock.m()) }
}

… but I don’t know if it covers all scenarios. The following test passes:

interface SomeInterface {

    suspend fun foo(): Int
}

@Test
fun test() = runBlocking {
    val m = mock<SomeInterface> {
        onBlocking { foo() } doReturn 42
    }

    val result = m.foo()

    expect(result).toBe(42)
}
7reactions
juanchosaraviacommented, Aug 31, 2018

Pointing to this version I was able to use directly the onBlocking function: “com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0-alpha04”

I tried using latest version: “com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0-RC1” but onBlocking is not available anymore and I don’t know if there is another function that is replacing this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
How to test suspend function using MockK? - Stack Overflow
runBlocking allows you to call suspend functions by blocking a new coroutine and it blocks the current thread until it is completed.
Read more >
Mocking Coroutines - ProAndroidDev
Except for the keyword suspend , this looks like is a synchronous function with a direct return type. Let's get back to Mockito...
Read more >
Coroutine/suspend function mocking support · Issue #205
If the mock and onGeneric lambdas were either suspend functions or inline, then I wouldn't need to put runBlocking inside the onGeneric for ......
Read more >
Coroutines and suspend functions | Mocking | MockK Guidebook
Using coEvery, coVerify, and more to mock coroutines. ... MockK uses function literals to create stubs, small changes are needed to stub suspend...
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