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.

io.mockk.MockKException: Failed matching mocking signature for SignedCall(...) left matchers: [any()] when running with RobolectricTestRunner

See original GitHub issue

Prerequisites

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Expected Behavior

The test passes without error.

Current Behavior

When attempting to mock a response for an extension function using mockkStatic and running the test with RobolectricTestRunner, the test fails to run due to MockKException.

Failure Information (for bugs)

Issue only occurs when using either the RobolectricTestRunner or AndroidJUnit4 test runner & the extension function has at least 1 input parameter. Note that when there are no input parameters, the test will still fail, but will not throw this exception.

Steps to Reproduce

  1. Create an extension function which takes at least 1 parameter to test. Example: fun TimeUnit.getSome(of: String): String = "GET SOME $of $this!"
  2. Create unit test and mock the response for the extension function
class ExampleUnitTest {
    @Test
    fun test() {
        mockkStatic("path.to.pkg.MyExtensionsKt")
        val testable: TimeUnit = mockk {
            every { getSome(any()) } returns "NONE!"
        }
        assertEquals(testable.getSome("of"), "NONE!")
    }
}
  1. Run test & verify it passes
  2. Change the test runner to use RobolectricTestRunner (i.e. @RunWith(RobolectricTestRunner::class))
  3. Run the test again & observe failure

Context

  • MockK version: 1.10.2
  • OS: MacOS Catalina 10.15.6 (19G2021)
  • Kotlin version: 1.3.72
  • JDK version: 11
  • JUnit version: 4.12
  • Robolectric version: 4.4
  • Type of test: UNIT

Failure Logs

io.mockk.MockKException: Failed matching mocking signature for
SignedCall(retValue=TimeUnit(#1), isRetValueMock=false, retType=class kotlin.String, self=TimeUnit(#1), method=toString(), args=[], invocationStr=TimeUnit(#1).toString())
left matchers: [any()]

Stack trace

io.mockk.MockKException: Failed matching mocking signature for
SignedCall(retValue=TimeUnit(#1), isRetValueMock=false, retType=class kotlin.String, self=TimeUnit(#1), method=toString(), args=[], invocationStr=TimeUnit(#1).toString())
left matchers: [any()]

	at io.mockk.impl.recording.SignatureMatcherDetector.detect(SignatureMatcherDetector.kt:99)
	at io.mockk.impl.recording.states.RecordingState.signMatchers(RecordingState.kt:39)
	at io.mockk.impl.recording.states.RecordingState.round(RecordingState.kt:31)
	at io.mockk.impl.recording.CommonCallRecorder.round(CommonCallRecorder.kt:50)
	at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:59)
	at io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:30)
	at io.mockk.MockKDsl.internalEvery(API.kt:92)
	at io.mockk.MockKKt.every(MockK.kt:98)
	at com.molchanov.android.sandbox.ExampleUnitTest.test(ExampleUnitTest.kt:22)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:575)
	at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:263)
	at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)


Process finished with exit code 255

Minimal reproducible code (the gist of this issue)

// -----------------------[ GRADLE DEFINITIONS ] -----------------------
dependencies {
    testImplementation 'junit:junit:4.12'
    testImplementation 'io.mockk:mockk:1.10.2'
    testImplementation 'org.robolectric:robolectric:4.4'
}
// -----------------------[ YOUR CODE STARTS HERE ] -----------------------
## MyExtensions.kt:
package com.molchanov.android.sandbox

import java.util.concurrent.TimeUnit

fun TimeUnit.getSome(of: String): String = "GET SOME $of $this!"

## UnitTest.kt:
package com.molchanov.android.sandbox

import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import java.util.concurrent.TimeUnit

@Config(manifest = Config.NONE)
@RunWith(RobolectricTestRunner::class)
class UnitTest {

    @Test
    fun test() {
        mockkStatic("com.molchanov.android.sandbox.MyExtensionsKt")
        val testable: TimeUnit = mockk {
            every { getSome(any()) } returns "NONE!"
        }
        assertEquals(testable.getSome("of"), "NONE!")
    }
}
// -----------------------[ YOUR CODE ENDS HERE ] -----------------------

Issue Analytics

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

github_iconTop GitHub Comments

7reactions
GladeDivineycommented, Feb 26, 2021

Still reproducible with Mockk 1.10.6 and Robolectric 4.5.1.

7reactions
BelooScommented, Feb 26, 2021

This issue is still reproducible, please check @Raibaz

Read more comments on GitHub >

github_iconTop Results From Across the Web

io.mockk.MockKException: Failed matching mocking signature ...
The any() object being requested has an Abstract Type argument. (i.e. MyObject<T> which is the same as calling any<MyObject<T>>() ).
Read more >
io.mockk.MockKException: Failed matching mocking signature for ...
io.mockk.MockKException: Failed matching mocking signature for SignedCall(...) left matchers: [any()] when running with RobolectricTestRunner.
Read more >
Io.Mockk.Mockkexception: Failed Matching Mocking ... - ADocLib
Failed matching mocking signature for left matchers 5 in the following way to match the generic typed class: I've recently been involved in...
Read more >
Failed matching mocking signature for SignedCall-kotlin
Coding example for the question io.mockk.MockKException: Failed matching mocking signature for SignedCall-kotlin.
Read more >
mockk-io/Lobby - Gitter
every { loginPresenter["trySaveLogin"](allAny<Any>()) } just Runs ... MockKException: Failed matching mocking signature for left matchers: [any(), any()].
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