Mockk: Error with Android Instrumented Tests Support

Created on 10 Jul 2018  路  4Comments  路  Source: mockk/mockk

Prerequisites

  • [Yes ] I am running the latest version
  • [ Yes] I checked the documentation and found no answer
  • [ Yes] I checked to make sure that this issue has not already been filed

Expected Behavior

According to documentation, mockk can be used in Android Instrumented test, also It can mockk objects in Android P devices.

Current Behavior

Running tests that use mockkObject or mockk in an Emulator with Android P, I get the following error Missing calls inside every { ... } block

Failure Information (for bugs)

When a matcher is used in every block, I get io.mockk.MockKException: Failed matching mocking signature for left matchers: [more(2), more(5)] error

Context

  • MockK version: 1.8.5
  • OS: Windows - Android P and Android O
  • Kotlin version: 1.2.41
  • JDK version: 8
  • Type of test: unit test and android instrumented test

Stack trace

Stack trace with matchers

io.mockk.MockKException: Failed matching mocking signature for

left matchers: [more(2), more(5)]
at io.mockk.impl.recording.SignatureMatcherDetector.detect(SignatureMatcherDetector.kt:99)
at io.mockk.impl.recording.states.RecordingState.signMatchers(RecordingState.kt:38)
at io.mockk.impl.recording.states.RecordingState.round(RecordingState.kt:30)
at io.mockk.impl.recording.CommonCallRecorder.round(CommonCallRecorder.kt:45)
at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:47)
at io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:25)
at io.mockk.MockKDsl.internalEvery(API.kt:93)
at io.mockk.MockKKt.every(MockK.kt:79).....

Stack trace without matchers

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 io.mockk.MockKDsl.internalEvery(API.kt:93)
at io.mockk.MockKKt.every(MockK.kt:79)....

Minimal reproducible code (the gist of this issue)

Example withouth matchers:

@RunWith(AndroidJUnit4::class)
class MockkIssueAndroidIntrumentedTest {
    object ObjectForMock {
        fun add(a:Int,b:Int)=a+b
    }
    class ClassForMock(){
        fun add(a:Int,b:Int)=a+b
    }
    @Test
    fun testForMockkObject() {


        mockkObject(ObjectForMock) // aplies mocking to an Object

        assertEquals(3, ObjectForMock.add(1, 2))

        every { ObjectForMock.add(1, 2) } returns 5

        assertEquals(5, ObjectForMock.add(1, 2))

        verifyAll {
            ObjectForMock.add(1, 2)
        }
    }

    @Test
    fun testForMock() {

        val mock = mockk<ClassForMock>()
        every { mock.add(1,2) } returns 5
        assertEquals(5, mock.add(1, 2))
        verifyAll {
            mock.add(1, 2)
        }
    }
}

Example with matchers:

@RunWith(AndroidJUnit4::class)
class MockkIssueAndroidIntrumentedTest {
    object ObjectForMock {
        fun add(a:Int,b:Int)=a+b
    }
    class ClassForMock(){
        fun add(a:Int,b:Int)=a+b
    }
    @Test
    fun testForMockkObject() {


        mockkObject(ObjectForMock) // aplies mocking to an Object

        assertEquals(3, ObjectForMock.add(1, 2))

        every { ObjectForMock.add(more(2), more(5)) } returns 10

        assertEquals(10, ObjectForMock.add(3, 6))

        verifyAll {
            ObjectForMock.add(3, 6)
        }
    }

    @Test
    fun testForMock() {

        val mock = mockk<ClassForMock>()
        every { mock.add(1,2) } returns 5
        assertEquals(5, mock.add(1, 2))
        verifyAll {
            mock.add(1, 2)
        }
    }
}
ait bug

All 4 comments

This was a very stupid bug because this place was tagged with FIXME and it was just incorrect version check.

Please check version 1.8.9 or 1.8.9.kotlin13. Should be much better now. Sorry for the long lead time. My laptop was too slow to debug it. Now it is a lot better, so I can fix AIT bugs quite easy.

Is this issue still open? I see Fixed AIT for Android P+ v1.8.9 and v1.8.9.kotlin13 #112 on mockk.io :thinking:

It is still not confirmed that it is fixed on user side

@oleksiyp v1.8.9 works correctly with AIT. Thank you so much, good job!

Was this page helpful?
0 / 5 - 0 ratings