Mockk Mocking Private Properties in Kotlin

Viewed 7742

I have a simple class with a private field.

class EmployeeData {

    private var employeeAge: Int = 0
    
    fun getAge(): Int {
        return 1 + employeeAge
    }
}

I am trying to test this private employeeAge with the following from official docs

@Test
fun testPrivateAge() {

    val mock = spyk(EmployeeData())

    every {
        mock getProperty "employeeAge"
    } propertyType Int::class answers { fieldValue + 6 }
    every {
        mock setProperty "employeeAge" value any<Int>()
    } propertyType Int::class answers  { fieldValue += value }


    every { mock getProperty "employeeAge" } returns 33
    every { mock setProperty "employeeAge" value less(5) } just Runs

    assertEquals(10,mock.getAge())
}

I am receiving such exception from MockK

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)

Any clue on what's I am doing wrong? Official docs suggest using such technique against private properties but for me it doesn't work and I'm using latest on this moment version of MockK which is v1.10.0.

Though for private methods it is working like a charm. I am able to test the private method in this logic.

1 Answers
Related