Mockk: Using @JvmStatic annotation makes Mockk to throw an exception

Created on 28 May 2018  路  4Comments  路  Source: mockk/mockk

First I want to thank you for this amazing library, as far as I know it's the only mock framework made purely with Kotlin.

Now the bad news :D

If an Object has one of its methods annotated with @JvmStatic, when trying to mock that method, an exception is thrown.

Code to reproduce the bug

object ObjectAnnotated {
    @JvmStatic fun foo() = "foo"
}
class ObjectAnnotatedTest {
    @Test
    fun verifyFoo() {
        objectMockk(ObjectAnnotated).mock()
        every { ObjectAnnotated.foo() } returns "bar"
        assert(ObjectAnnotated.foo() == "bar")
    }
}

Stacktrace

io.mockk.MockKException: Missing calls inside every { ... } block.
    at io.mockk.impl.recording.states.StubbingState.checkMissingCalls(StubbingState.kt:14)
    at io.mockk.impl.recording.states.StubbingState.recordingDone(StubbingState.kt:8)
    at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:42)
    at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:48)
    at io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:25)
    at com.mufumbo.android.recipe.search.provider_login.ObjectAnnotatedTest.verifyFoo(ObjectAnnotatedTest.kt:25)

I'm using latest version -> 1.8

Please let me know if you need further support.

help wanted

Most helpful comment

It works, thanks @oleksiyp for the support 馃憤

All 4 comments

Just try static mock on that object class

I don't know what you mean by static mock. Can you share the implementation based on my previous example?

Can't check right now this code(hope it works):

class ObjectAnnotatedTest {
    @Test
    fun verifyFoo() {
        staticMockk<ObjectAnnotated>().use {
            every { ObjectAnnotated.foo() } returns "bar"
            assert(ObjectAnnotated.foo() == "bar")
       }
    }
}

It works, thanks @oleksiyp for the support 馃憤

Was this page helpful?
0 / 5 - 0 ratings