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.

any() doesn't work for kotlin.Unit

See original GitHub issue

Just be aware about usual MockK support pattern. Tickets are checked from time to time, replied, discussed, labeled, e.t.c. But real fixes are applied in a month-two month period in a bunch. If you think this is unacceptable, go on, join the project, change the world.

Please remove sections wisely

Below information is actually needed to make all the process of fixing faster. Choose main points. Don’t blindly follow this as a set of rules. Don’t waste much time. Usually, the main thing is to have a good reproducible minimal code.

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • 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

when I mock a funtion which has a Unit argument by every { mockObject.function(any()) }, it should return expected value.

Current Behavior

fail because of fail matching mocking signature

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • MockK version:1.12.0
  • OS: Windows 10
  • Kotlin version: 1.7.10
  • JDK version: 1.8
  • JUnit version: JUnit 4
  • Type of test: unit test

Stack trace

Failed matching mocking signature for
SignedCall(retValue=, isRetValueMock=true, retType=class kotlin.Unit, self=MyClass(#1), method=abc(Unit), args=[kotlin.Unit], invocationStr=MyClass(#1).abc(kotlin.Unit))
left matchers: [any()]
io.mockk.MockKException: Failed matching mocking signature for
SignedCall(retValue=, isRetValueMock=true, retType=class kotlin.Unit, self=MyClass(#1), method=abc(Unit), args=[kotlin.Unit], invocationStr=MyClass(#1).abc(kotlin.Unit))
left matchers: [any()]
	at app//io.mockk.impl.recording.SignatureMatcherDetector.detect(SignatureMatcherDetector.kt:99)
	at app//io.mockk.impl.recording.states.RecordingState.signMatchers(RecordingState.kt:39)
	at app//io.mockk.impl.recording.states.RecordingState.round(RecordingState.kt:31)
	at app//io.mockk.impl.recording.CommonCallRecorder.round(CommonCallRecorder.kt:50)
	at app//io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:63)
	at app//io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:30)
	at app//io.mockk.MockKDsl.internalEvery(API.kt:92)
	at app//io.mockk.MockKKt.every(MockK.kt:98)
	at app//cn.futu.news.maintab.optional.optionalreport.presentation.viewmodel.MyTest.testOne(MyTest.kt:17)
	at java.base@11/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base@11/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base@11/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base@11/java.lang.reflect.Method.invoke(Method.java:566)
	at app//org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at app//org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at app//org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at app//org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at app//org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at app//org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at app//org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at app//org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at app//org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at app//org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at app//org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at app//org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at app//org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
	at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62)
	at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
	at java.base@11/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base@11/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base@11/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base@11/java.lang.reflect.Method.invoke(Method.java:566)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
	at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
	at com.sun.proxy.$Proxy5.processTestClass(Unknown Source)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker$2.run(TestWorker.java:176)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:129)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:100)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:60)
	at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56)
	at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:133)
	at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:71)
	at app//worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
	at app//worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)

Minimal reproducible code (the gist of this issue)

class MyTest {

    @Test
    fun testOne() {
        val ob = mockk<MyClass>()
       // this statement will cause Failed matching mocking signature
        every { ob.abc(any()) } answers {
            println("Mock~")
        }

        ob.abc(Unit)
    }
}

class MyClass {
    fun abc(a: Unit) {
        println("abc")
    }
}

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
yoshimuratakuma0commented, Aug 27, 2022
class MyTest {

    @Test
    fun testOne() {
        val ob = mockk<MyClass>()
       // this statement will cause Failed matching mocking signature
       // every {ob.abc(any() } answers {
        every { ob.abc(Unit) } answers {
            println("Mock~")
        }

        ob.abc(Unit)
    }
}

class MyClass {
    fun abc(a: Unit) {
        println("abc")
    }
}

↑ I can pass the test. It might be the cause that Unit is not an instance but singleton.

1reaction
ZeroXsLabcommented, Aug 15, 2022

It is for this case…

First of all, we got some executable things. Accounding to the Interface-Segregation Principles, we define a executable interface like

interface Executable<in TParam, out TResult> {
    fun execute(param: TParam): TResult
}

Normally, the param is a Int, String, or any other class, and the MockK works well.

But in some case, the executable things doesn’t need any param, so we define it like the abc function above. And in this case, the MockK is failed to mock and return the expected value.

class NoParamExecutableThings: Executable<Unit, Sting> {
    // for this case, we don't need any param
    // but the Executable interface requires a specific class type, so it's `Unit`
    override fun execute(param: Unit): String { ... }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mockito when clause not working in kotlin - Stack Overflow
Without adding another dependency, you can replace using the @Mock annotation with a helper function somewhere:
Read more >
Kotlin.Unit error - Support
Guys am a beginner and i can't undertsand why; when i run this code i get “Kotlin.Unit” message under the solution !! i...
Read more >
`void` methods | Migrating from Mockito | MockK Guidebook
Mockito's when method doesn't work with void methods. ... MockK doesn't have any restrictions with these methods, as they return Unit in Kotlin....
Read more >
Void Type in Kotlin | Baeldung on Kotlin
If a function's return type is Nothing then that function doesn't return any value not even the default return type Unit. For example,...
Read more >
[Kotlin Pearls 7] Unit, Nothing, Any (and null) - ProAndroidDev
Looking at the code for Any , we can also see that is one of the few classes in the Kotlin stdlib that...
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