# Verifying suspending functions with Mockito (or alternatives)

**URL:** https://discuss.kotlinlang.org/t/verifying-suspending-functions-with-mockito-or-alternatives/2492
**Category:** Support
**Created:** [April 10, 2017, 9:22pm UTC](https://discuss.kotlinlang.org/t/verifying-suspending-functions-with-mockito-or-alternatives/2492 "2017-04-10T21:22:24Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![mspain](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mspain](https://discuss.kotlinlang.org/u/mspain)
#### Post date: [April 10, 2017, 9:22pm UTC](https://discuss.kotlinlang.org/t/verifying-suspending-functions-with-mockito-or-alternatives/2492/1 "2017-04-10T21:22:24Z")

</div>

I’ve run into trouble using Mockito to verify invocations of suspending functions. This appears to be related to how suspending functions get translated for the JVM, which I can at least determine involves an additional parameter. Because Mockito checks all the arguments passed to the mock’s Java methods, the verification fails, since the last argument from the invocation does not match that from the verification.

Here is a simple example:

```
interface Suspendable {
    suspend fun suspendFunction()
}

class CallsSuspendable(val suspendable: Suspendable) {
    fun callSuspendable() {
        runBlocking {
            suspendable.suspendFunction()
        }
    }
}

@Test
fun testCallingSuspendable() {
    val mockSuspendable = Mockito.mock(Suspendable::class.java)
    val callsSuspendable = CallsSuspendable(mockSuspendable)

    callsSuspendable.callSuspendable()

    runBlocking {
        Mockito.verify(mockSuspendable).suspendFunction()
    }
}

```

In this example, I get the error:

> Argument(s) are different! Wanted:  
> interface.suspendable(  
> (testCallingSuspendable$1) kotlinx.coroutines.experimental.CoroutineScope.() → kotlin.Unit  
> );  
> → at MockSuspendFunctionTest$testCallingSuspendable$1.doResume(MockSuspendFunctionTest.kt:38)  
> Actual invocation has different arguments:  
> interface.suspendable(  
> (useSuspendable$1) kotlinx.coroutines.experimental.CoroutineScope.() → kotlin.Unit  
> );  
> → at MockSuspendFunctionTest$UsesSuspendable$useSuspendable$1.doResume(MockSuspendFunctionTest.kt:25)

Any other ideas how to verify invocation of a suspend function? I imagine there may be some kind of tests like this in the Kotlin project itself.

---

<div class="post-metadata">

### Author: ![elizarov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/elizarov/32/2164_2.png) [@elizarov](https://discuss.kotlinlang.org/u/elizarov)
#### Post date: [April 11, 2017, 1:42pm UTC](https://discuss.kotlinlang.org/t/verifying-suspending-functions-with-mockito-or-alternatives/2492/2 "2017-04-11T13:42:40Z")

</div>

I don’t think there is any kind of good solution to this problem, but to teach Mockito to understand Kotlin suspending functions. There are many other problems beyond verification. Mockito matchers don’t work properly either, due to this extra hidden parameter. It looks like it requires a patch to Mockito itself.

The only only workaround I can see is this.

First, you’ll have to define another interface that defines the corresponding functions as non-suspending:

```auto
interface SuspendableMock {
    fun suspendFunctionMock()
}

```

Then you’ll have to create an implementation that implements your suspendable interface with delegation to the above non-suspendable interface:

```auto
class SuspendableImpl(val mock: SuspendableMock) : Suspendable {
    suspend override fun suspendFunction() = mock.suspendFunctionMock()
}

```

Then you can mock `SuspendableMock` with Mockito normally (the following test passes):

```auto
@Test
fun testCallingSuspendable() {
    val mockSuspendable = Mockito.mock(SuspendableMock::class.java)
    val callsSuspendable = CallsSuspendable(SuspendableImpl(mockSuspendable))

    callsSuspendable.callSuspendable()

    runBlocking {
        Mockito.verify(mockSuspendable).suspendFunctionMock()
    }
}

```

---

<div class="post-metadata">

### Author: ![mspain](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mspain](https://discuss.kotlinlang.org/u/mspain)
#### Post date: [April 11, 2017, 3:32pm UTC](https://discuss.kotlinlang.org/t/verifying-suspending-functions-with-mockito-or-alternatives/2492/3 "2017-04-11T15:32:38Z")

</div>

Well, that’s certainly better than making my own mocking library from scratch to support suspending functions. If you’ve got a bunch of non-suspending functions in your interface as well, class delegation saves you at least a little bit of boilerplate over manually delegating every function call:

```
interface Suspendable {
    suspend fun suspendFunction()
    fun blockingFunction1()
    fun blockingFunction2()
}

interface SuspendableMock {
    fun suspendFunction_()
    fun blockingFunction1()
    fun blockingFunction2()
}

class SuspendableImpl(val mock: SuspendableMock) : Suspendable, SuspendableMock by mock {
    suspend override fun suspendFunction() = mock.suspendFunction_()
}

```

---

<div class="post-metadata">

### Author: ![elizarov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/elizarov/32/2164_2.png) [@elizarov](https://discuss.kotlinlang.org/u/elizarov)
#### Post date: [April 12, 2017, 11:51am UTC](https://discuss.kotlinlang.org/t/verifying-suspending-functions-with-mockito-or-alternatives/2492/4 "2017-04-12T11:51:42Z")

</div>

I’ve also submitted a pull request for Mockito that fixes this problem: [Support for Kotlin suspending functions by elizarov · Pull Request #1032 · mockito/mockito · GitHub](https://github.com/mockito/mockito/pull/1032)

Hopefully, it is going to be accepted into the next version.

---

<div class="post-metadata">

### Author: ![mspain](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mspain](https://discuss.kotlinlang.org/u/mspain)
#### Post date: [April 12, 2017, 12:43pm UTC](https://discuss.kotlinlang.org/t/verifying-suspending-functions-with-mockito-or-alternatives/2492/5 "2017-04-12T12:43:48Z")

</div>

Very cool, thank you.

---

<div class="post-metadata">

### Author: ![mspain](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mspain](https://discuss.kotlinlang.org/u/mspain)
#### Post date: [May 8, 2017, 5:41pm UTC](https://discuss.kotlinlang.org/t/verifying-suspending-functions-with-mockito-or-alternatives/2492/6 "2017-05-08T17:41:41Z")

</div>

I saw your changes were merged in to Mockito. Thanks for taking care of that.

---

<div class="post-metadata">

### Author: ![clintam](https://avatars.discourse-cdn.com/v4/letter/c/8491ac/32.png) [@clintam](https://discuss.kotlinlang.org/u/clintam)
#### Post date: [August 27, 2018, 10:31pm UTC](https://discuss.kotlinlang.org/t/verifying-suspending-functions-with-mockito-or-alternatives/2492/7 "2018-08-27T22:31:10Z")

</div>

Hi there,

I’m having a similar issue that happens when verifying suspending functions that end up yielding. Follows is a simple example:

```auto
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.runBlocking
import org.junit.jupiter.api.Test
import org.mockito.Mockito

class SuspendTest {
    class Suspendable {
        suspend fun suspendFunction() {
            delay(1)
            async { delay(1) }.await()
        }
    }

    class CallsSuspendable(val suspendable: Suspendable) {
        suspend fun callSuspendable() {
            suspendable.suspendFunction()

        }
    }

    @Test
    fun testCallingSuspendable() = runBlocking<Unit> {
        val suspendable = Suspendable()
        val verifySuspendable = Mockito.spy(suspendable)
        val callsSuspendable = CallsSuspendable(verifySuspendable)

        callsSuspendable.callSuspendable()

        Mockito.verify(verifySuspendable, Mockito.times(1)).suspendFunction()
    }
}

```

When I run this, I get

```auto
org.mockito.exceptions.verification.TooManyActualInvocations: 
suspendable.suspendFunction();
Wanted 1 time:
-> at SuspendTest$Suspendable.suspendFunction(SuspendTest.kt:11)
But was 2 times:
-> at SuspendTest$CallsSuspendable.callSuspendable(SuspendTest.kt:19)
-> at SuspendTest$Suspendable$suspendFunction$1.doResume(SuspendTest.kt)

```

We are trying to write an integration test that spies on the real coroutines so they end up yielding and Mockito interprets the resume as another invocation.

Any ideas on how to achieve this, or if this behaviour is fixable in Mockito ? Thanks!

---

<div class="post-metadata">

### Author: ![farruh.habibullaev](https://avatars.discourse-cdn.com/v4/letter/f/e495f1/32.png) [@farruh.habibullaev](https://discuss.kotlinlang.org/u/farruh.habibullaev)
#### Post date: [July 27, 2019, 4:35am UTC](https://discuss.kotlinlang.org/t/verifying-suspending-functions-with-mockito-or-alternatives/2492/8 "2019-07-27T04:35:39Z")

</div>

I have encountered the same problem now. Is PR accepted now?

---

<div class="post-metadata">

### Author: ![imanushin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/imanushin/32/5471_2.png) [@imanushin](https://discuss.kotlinlang.org/u/imanushin)
#### Post date: [July 29, 2019, 8:48am UTC](https://discuss.kotlinlang.org/t/verifying-suspending-functions-with-mockito-or-alternatives/2492/9 "2019-07-29T08:48:06Z")

</div>

As temporary workaround I suggest to take a look on the [mockk](https://github.com/mockk/mockk) (it works for me)

```auto
val mockedService = mockk<SomeInterface> {
    coEvery { suspendFunction(any()) } returns listOf(someVariable1, someVariable2)
}

```
