Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: cannot capture nullable argument in mocked suspending methods #354

Open
rocca23 opened this issue Sep 17, 2019 · 4 comments
Open

Bug: cannot capture nullable argument in mocked suspending methods #354

rocca23 opened this issue Sep 17, 2019 · 4 comments

Comments

@rocca23
Copy link

rocca23 commented Sep 17, 2019

Example code:

@Test
    fun foo() {
        val arg = mutableListOf<String?>()
        val server = mockk<Server> {
            coEvery { foo(captureNullable(arg)) } just runs
        }
        runBlocking {
            server.foo()
        }
        assertNull(arg.last())
    }

Gist with full code

Executing the test produces this exception:

io.mockk.MockKException: no answer found for: Server(#1).foo(null, continuation {})
at io.mockk.impl.stub.MockKStub.defaultAnswer(MockKStub.kt:90)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:42)
at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:53)
at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:263)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:25)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
at io.mockk.proxy.jvm.advice.BaseAdvice.handle(BaseAdvice.kt:42)
at io.mockk.proxy.jvm.advice.jvm.JvmMockKProxyInterceptor.interceptNoSuper(JvmMockKProxyInterceptor.java:45)
at com.app.test.Server$Subclass0.foo(Unknown Source)
at com.app.test.Server$DefaultImpls.foo$default(UnitTests.kt:43)
at com.app.test.UnitTests$foo$1.invokeSuspend(UnitTests.kt:35)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
...

Replacing captureNullable(arg) with any(), or making arg not-null and using capture(arg), both don't produce the error.

Context

  • MockK version: 1.9.3
  • Kotlin version: 1.3.50 (coroutines 1.3.1)
  • JUnit version: 4
  • Type of test: both in Instrumented test (Android 9) and Unit test (JRE: 1.8.0_202-release-1483-b49-5587405 amd64; JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o; Linux 5.0.0-27-generic).
@rocca23
Copy link
Author

rocca23 commented Sep 17, 2019

Must be similar to the recent #353 . Also, I was trying to work this around by using verify and withNullableArg, but the latter is broken with null values as per #323 .
At the moment it looks like it's impossible to verify null arguments in tests.

@oleksiyp oleksiyp changed the title Cannot capture nullable argument in mocked suspending methods Bug: cannot capture nullable argument in mocked suspending methods Nov 2, 2019
@oleksiyp oleksiyp added this to To do in Critical to fix Nov 2, 2019
@nathanmalloch
Copy link

sadly this is still an issue. is there a work around for this for now?

@michaelliangbrex
Copy link

Is the issue fixed?

@whck6
Copy link

whck6 commented Oct 27, 2023

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

5 participants