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.

Matchers are not working for suspend functions

See original GitHub issue

It seems that if you try to use “matchers” (any, eq, etc.) on suspend function mockito fails with “InvalidUseOfMatchersException”.

following test

@Test
fun otherTest() {
    val foo: Foo = mock {
        on { runBlocking { bar(any()) } } doReturn ("message")
    }
    Mockito.validateMockitoUsage()
}

with following interface definition

interface Foo {
    suspend fun bar(arg: String): String
}

produces following error

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at org.app.Tests$otherTest$i$1$1$1.doResume(Tests.kt:76)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:15
  • Comments:19

github_iconTop GitHub Comments

5reactions
xDPacommented, Jan 8, 2021

same problem as @lborisevich-clgx . high order functions doesn’t seem to work:

val myFunction = mock<suspend (Int) -> Boolean>()

myFunction.stub {
    onBlocking { invoke(any()) }.doReturn(false)
}
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded:

but works with zero parameters:

val myFunction = mock<suspend () -> Boolean>()

myFunction.stub {
    onBlocking { invoke() }.doReturn(false)
}
2reactions
andymooncommented, Dec 11, 2018

I had to set my dependencies as below in my gradle file for the matchers to work within a suspend.

dependencies {
            testCompile 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.0.0'
            testCompile("org.mockito:mockito-core:2.23.0")
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Matchers are not working for suspend functions #269 - GitHub
It seems that if you try to use "matchers" (any, eq, etc.) on suspend function mockito fails with "InvalidUseOfMatchersException". following ...
Read more >
Verify suspend function with Matchers.anyObject()
MyTest$testFunction$1.invokeSuspend(MyTest.kt:66) This exception may occur if matchers are combined with raw values: //incorrect: someMethod ...
Read more >
Verifying suspending functions with Mockito (or alternatives)
I've run into trouble using Mockito to verify invocations of suspending functions. This appears to be related to how suspending functions ...
Read more >
Suspending functions should not be called on a different ...
Kotlin static code analysis: Suspending functions should not be called on a different dispatcher.
Read more >
Coroutines and suspend functions | Mocking | MockK Guidebook
Coroutines and suspend functions #. As MockK uses function literals to create stubs, small changes are needed to stub suspend functions.
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