any() doesn't work for kotlin.Unit
See original GitHub issueJust 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:
- Created a year ago
- Reactions:1
- Comments:5
Top GitHub Comments
↑ I can pass the test. It might be the cause that Unit is not an instance but singleton.
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
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.