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.

kotlin.KotlinNullPointerException error when I try to use any() method on a value class parameter

See original GitHub issue

When I try to use any() method on a parameter with a value class as type, I have kotlin.KotlinNullPointerException error.

Expected Behavior

The test should run correctly and the mock can intercept all instance of the value class parameter.

Current Behavior

Generate a kotlin.KotlinNullPointerException error on test execution I don’t have the issue if I replace the value class with data class and I don’t have the issue if I replace the any() call by a explicit instance of the value class.

Steps to Reproduce

See the code below.

Context

  • MockK version: 1.12.0
  • OS: MacOs 11.5.2 (20G95)
  • Kotlin version: 1.5.21, 1.5.30
  • JDK version: OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9)
  • JUnit version: 5.6.2 (if I belived the depencies tree in IntelliJ)
  • Kotest version : 4.6.1, 4.6.2
  • Type of test: unit test

Failure Logs

Please include any relevant log snippets or files here.

Stack trace

Test failed
kotlin.KotlinNullPointerException
	at io.mockk.impl.recording.states.RecordingState.matcher(RecordingState.kt:53)
	at io.mockk.impl.recording.CommonCallRecorder.matcher(CommonCallRecorder.kt:52)
	at org.test.HelloTest$1$1$1.invoke(HelloTest.kt:22)
	at org.test.HelloTest$1$1$1.invoke(HelloTest.kt:12)
	at io.mockk.impl.eval.RecordedBlockEvaluator$record$block$1.invoke(RecordedBlockEvaluator.kt:25)
	at io.mockk.impl.eval.RecordedBlockEvaluator$enhanceWithRethrow$1.invoke(RecordedBlockEvaluator.kt:78)
	at io.mockk.impl.recording.JvmAutoHinter.autoHint(JvmAutoHinter.kt:23)
	at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:40)
	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 org.test.HelloTest$1$1.invokeSuspend(HelloTest.kt:12)
	at org.test.HelloTest$1$1.invoke(HelloTest.kt)
	at org.test.HelloTest$1$1.invoke(HelloTest.kt)
	at io.kotest.core.internal.ExecutionsKt$executeWithBehaviours$2$1.invokeSuspend(executions.kt:13)
	at io.kotest.core.internal.ExecutionsKt$executeWithBehaviours$2$1.invoke(executions.kt)
	at io.kotest.core.internal.ExecutionsKt$executeWithBehaviours$2$1.invoke(executions.kt)
	at io.kotest.core.internal.ExecutionsKt.wrapTestWithGlobalAssert(executions.kt:39)
	at io.kotest.core.internal.ExecutionsKt.access$wrapTestWithGlobalAssert(executions.kt:1)
	at io.kotest.core.internal.ExecutionsKt$executeWithBehaviours$2.invokeSuspend(executions.kt:12)
	at io.kotest.core.internal.ExecutionsKt$executeWithBehaviours$2.invoke(executions.kt)
	at io.kotest.core.internal.ExecutionsKt$executeWithBehaviours$2.invoke(executions.kt)
	at io.kotest.core.internal.ExecutionsKt$wrapTestWithAssertionModeCheck$2.invokeSuspend(executions.kt:25)
	at io.kotest.core.internal.ExecutionsKt$wrapTestWithAssertionModeCheck$2.invoke(executions.kt)
	at io.kotest.core.internal.ExecutionsKt$wrapTestWithAssertionModeCheck$2.invoke(executions.kt)
	at io.kotest.core.internal.AssertionsCheckKt.executeWithAssertionsCheck(assertionsCheck.kt:25)
	at io.kotest.core.internal.ExecutionsKt.wrapTestWithAssertionModeCheck(executions.kt:24)
	at io.kotest.core.internal.ExecutionsKt.executeWithBehaviours(executions.kt:11)
	at io.kotest.core.internal.TestCaseExecutor$executeInScope$2.invokeSuspend(TestCaseExecutor.kt:268)
	at io.kotest.core.internal.TestCaseExecutor$executeInScope$2.invoke(TestCaseExecutor.kt)
	at io.kotest.core.internal.TestCaseExecutor$executeInScope$2.invoke(TestCaseExecutor.kt)
	at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:89)
	at kotlinx.coroutines.CoroutineScopeKt.coroutineScope(CoroutineScope.kt:264)
	at io.kotest.core.internal.TestCaseExecutor.executeInScope(TestCaseExecutor.kt:262)
	at io.kotest.core.internal.TestCaseExecutor.access$executeInScope(TestCaseExecutor.kt:57)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4$1$1$3$1$1.invokeSuspend(TestCaseExecutor.kt:234)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4$1$1$3$1$1.invoke(TestCaseExecutor.kt)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4$1$1$3$1$1.invoke(TestCaseExecutor.kt)
	at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturnIgnoreTimeout(Undispatched.kt:100)
	at kotlinx.coroutines.TimeoutKt.setupTimeout(Timeout.kt:148)
	at kotlinx.coroutines.TimeoutKt.withTimeout(Timeout.kt:44)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4$1$1$3$1.invokeSuspend(TestCaseExecutor.kt:233)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4$1$1$3$1.invoke(TestCaseExecutor.kt)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4$1$1$3$1.invoke(TestCaseExecutor.kt)
	at io.kotest.engine.ExecutorExecutionContext$executeWithTimeoutInterruption$4.invokeSuspend(ExecutorExecutionContext.kt:77)
	at io.kotest.engine.ExecutorExecutionContext$executeWithTimeoutInterruption$4.invoke(ExecutorExecutionContext.kt)
	at io.kotest.engine.ExecutorExecutionContext$executeWithTimeoutInterruption$4.invoke(ExecutorExecutionContext.kt)
	at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:89)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:165)
	at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
	at io.kotest.engine.ExecutorExecutionContext.executeWithTimeoutInterruption(ExecutorExecutionContext.kt:75)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4$1$1$3.invokeSuspend(TestCaseExecutor.kt:232)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4$1$1$3.invoke(TestCaseExecutor.kt)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4$1$1$3.invoke(TestCaseExecutor.kt)
	at io.kotest.mpp.ReplayKt.replay(replay.kt:18)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4$1$1.invokeSuspend(TestCaseExecutor.kt:227)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4$1$1.invoke(TestCaseExecutor.kt)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4$1$1.invoke(TestCaseExecutor.kt)
	at io.kotest.engine.ExecutorExecutionContext$executeWithTimeoutInterruption$4.invokeSuspend(ExecutorExecutionContext.kt:77)
	at io.kotest.engine.ExecutorExecutionContext$executeWithTimeoutInterruption$4.invoke(ExecutorExecutionContext.kt)
	at io.kotest.engine.ExecutorExecutionContext$executeWithTimeoutInterruption$4.invoke(ExecutorExecutionContext.kt)
	at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:89)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:165)
	at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
	at io.kotest.engine.ExecutorExecutionContext.executeWithTimeoutInterruption(ExecutorExecutionContext.kt:75)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4$1.invokeSuspend(TestCaseExecutor.kt:220)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4$1.invoke(TestCaseExecutor.kt)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4$1.invoke(TestCaseExecutor.kt)
	at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturnIgnoreTimeout(Undispatched.kt:100)
	at kotlinx.coroutines.TimeoutKt.setupTimeout(Timeout.kt:148)
	at kotlinx.coroutines.TimeoutKt.withTimeout(Timeout.kt:44)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4.invokeSuspend(TestCaseExecutor.kt:219)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4.invoke(TestCaseExecutor.kt)
	at io.kotest.core.internal.TestCaseExecutor$executeAndWait$4.invoke(TestCaseExecutor.kt)
	at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:89)
	at kotlinx.coroutines.SupervisorKt.supervisorScope(Supervisor.kt:57)
	at io.kotest.core.internal.TestCaseExecutor.executeAndWait(TestCaseExecutor.kt:215)
	at io.kotest.core.internal.TestCaseExecutor.invokeTestCase(TestCaseExecutor.kt:184)
	at io.kotest.core.internal.TestCaseExecutor.executeActiveTest(TestCaseExecutor.kt:153)
	at io.kotest.core.internal.TestCaseExecutor.access$executeActiveTest(TestCaseExecutor.kt:57)
	at io.kotest.core.internal.TestCaseExecutor$intercept$innerExecute$1$1.invokeSuspend(TestCaseExecutor.kt:89)
	at io.kotest.core.internal.TestCaseExecutor$intercept$innerExecute$1$1.invoke(TestCaseExecutor.kt)
	at io.kotest.core.internal.TestCaseExecutor$intercept$innerExecute$1$1.invoke(TestCaseExecutor.kt)
	at io.kotest.core.internal.TestCaseExecutor.executeIfEnabled(TestCaseExecutor.kt:117)
	at io.kotest.core.internal.TestCaseExecutor.access$executeIfEnabled(TestCaseExecutor.kt:57)
	at io.kotest.core.internal.TestCaseExecutor$intercept$innerExecute$1.invokeSuspend(TestCaseExecutor.kt:89)
	at io.kotest.core.internal.TestCaseExecutor$intercept$innerExecute$1.invoke(TestCaseExecutor.kt)
	at io.kotest.core.internal.TestCaseExecutor$intercept$innerExecute$1.invoke(TestCaseExecutor.kt)
	at io.kotest.core.internal.TestCaseExecutor.intercept(TestCaseExecutor.kt:103)
	at io.kotest.core.internal.TestCaseExecutor.execute(TestCaseExecutor.kt:69)
	at io.kotest.engine.spec.runners.SingleInstanceSpecRunner.runTest(SingleInstanceSpecRunner.kt:104)
	at io.kotest.engine.spec.runners.SingleInstanceSpecRunner.access$runTest(SingleInstanceSpecRunner.kt:34)
	at io.kotest.engine.spec.runners.SingleInstanceSpecRunner$execute$interceptAndRun$2$4.invokeSuspend(SingleInstanceSpecRunner.kt:57)
	at io.kotest.engine.spec.runners.SingleInstanceSpecRunner$execute$interceptAndRun$2$4.invoke(SingleInstanceSpecRunner.kt)
	at io.kotest.engine.spec.runners.SingleInstanceSpecRunner$execute$interceptAndRun$2$4.invoke(SingleInstanceSpecRunner.kt)
	at io.kotest.engine.launchers.SequentialTestLauncher$launch$3$1$1.invokeSuspend(SequentialTestLauncher.kt:22)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
	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:829)

Minimal reproducible code (the gist of this issue)

plugins {
    kotlin("jvm") version "1.5.21"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation(kotlin("stdlib"))
    testImplementation("io.kotest:kotest-runner-junit5:4.6.1")
    testImplementation("io.mockk:mockk:1.12.0")
}
package org.test

@JvmInline
value class Name(val value: String)

interface Printer {
    fun print(name: Name): String
}

class Hello(private val printer: Printer) {
    fun hello(name: String) {
        println(printer.print(Name(name)))
    }
}
package org.test

import io.kotest.core.spec.style.ShouldSpec
import io.mockk.every
import io.mockk.mockk

class HelloTest : ShouldSpec({
    val mockPrinter = mockk<Printer>()

    should("print message") {
        every { mockPrinter.print(any()) } returns "OK"

        val hello = Hello(mockPrinter)

        hello.hello("TEST")
    }

})

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:10
  • Comments:5

github_iconTop GitHub Comments

5reactions
adrinietocommented, Oct 15, 2021

I have the same issue with 1.12.0. As a workaround instead of

every { mockPrinter.print(any()) } returns "OK"

you can use

every { mockPrinter.print(Name(any())) } returns "OK"
0reactions
stale[bot]commented, Apr 16, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as stale just ask to put an important label.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin - why do I get a KotlinNullPointerException
-operator throws an exception when the value is null . One way to trigger writeMessage safely would be to use .let : fun...
Read more >
Kotlin NullPointerException: Parameter specified as non-null ...
In this quick tutorial, we're going to see what happens when we store a null value into a non-null data type in Kotlin....
Read more >
KotlinNullPointerException when using capture() for an inline ...
When using capture() for an inline class parameter it should work. ... A in Kotlin; Create a class B that use the A...
Read more >
Kotlin Null Safety - GeeksforGeeks
If we try to assign null to the variable, it gives compiler error. ... Kotlin has a run() method to execute some operation...
Read more >
Null safety - Kotlin
Such a chain returns null if any of the properties in it is null . To perform a certain operation only for non-null...
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