https://kotlinlang.org logo
#mockk
Title
# mockk
t

tim

02/09/2022, 8:27 PM
I’m getting a NPE when I use a matcher on a
value class
, is this a known issue?
Example test:
Copy code
package glimpse.publish.unit

import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.booleans.shouldBeFalse
import io.mockk.every
import io.mockk.mockk

@JvmInline
value class Baz(val v: String)

class Foo {
    fun bar(baz: Baz) = true
}

class ExampleTest : FreeSpec({
    val foo = mockk<Foo>()

    beforeSpec {
        every {
            foo.bar(Baz("")) // this works
        } returns false

        every {
            foo.bar(any()) // this throws a NPE
        } returns false
    }

    "doesn't run because of NPE inside every { ... }" {
        foo
            .bar(Baz(""))
            .shouldBeFalse()
    }
})
Stack trace:
Copy code
java.lang.NullPointerException
at io.mockk.impl.recording.states.RecordingState.matcher(RecordingState.kt:53)
at io.mockk.impl.recording.CommonCallRecorder.matcher(CommonCallRecorder.kt:52)
at glimpse.publish.unit.ExampleTest$1$1$2.invoke(ExampleTest.kt:35)
at glimpse.publish.unit.ExampleTest$1$1$2.invoke(ExampleTest.kt:23)
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:93)
at io.mockk.MockKKt.every(MockK.kt:98)
e

ephemient

02/10/2022, 10:01 AM
t

tim

02/10/2022, 11:08 AM
Thanks! not sure how i missed the first issue last night 😞
3 Views