Home > Software engineering >  Kotlin. Mockk: Missing mocked calls inside every
Kotlin. Mockk: Missing mocked calls inside every

Time:07-21

I'm using mockk library fo testing my business logic. I have interactor which I want to test. My interactor retrieve in constructor data class. Fox example:

 data class Test(
        var data1: Data1? = null,
        var data2: Data2? = null,
        var data3: Data3? = null
        // etc
    )


    data class Data1(var nestedData: String)
    data class Data2(var test: String)
    data class Data3(var test: String)

Inside my test I'm trying to make partial mocking:

  @MockK
  private lateinit var test: Test 

  @Test
  fun `test`() {
         every { test } returns mockk {
            every { data1 } returns mockk {
                every { nestedData } returns "test"
            }
        }

}

And I get error:

Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock io.mockk.MockKException: Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock

I also tried to write like this:

   every { test } returns mockk {
            every { data1 } returns Data1("test")
        }

And I get same error. @RelaxedMockK also not working.

Why is this happening ? Please help me.

CodePudding user response:

You're mocking it twice and you don't need that. Remove the first mock and you will have something like that:

    private lateinit var test: Test

    @Test
    fun `test`() {
        test = mockk(relaxed = true) {
            every { data1 } returns mockk {
                every { nestedData } returns "test"
            }
        }
    }

If you need to have the same test set up for each test, you can move this part of creating a mocked object to @Before function.

  • Related