1

I'm using Mockk and I want to test a MediatorLiveData that depends of some boolean properties of the class.

I was using mockkConstructor(Boolean::class) but suddenly a warning appears on the console log and all the test cases fails (I'm not sure but seems to be happening after updating the Kotlin version)

WARNING: Non instrumentable classes(skipped): boolean

Class to test

class Testeando {
    var testBool = false
    fun test() : Boolean {
        return testBool
    }

}

This is the minimum possible code to replicate the error (Not the real test). the line of mocking the value of testBool is ignored.

@Test
fun `Just a test`() {
   mockkConstructor(Boolean::class)
   val t =spyk(Testeando())
   every{t.testBool }returns true
   assertTrue(t.test())
}

What I'm doing wrong?

1 Answers1

1

It seems like you cannot mock the Boolean class, I get an exception (mockk version 1.10.6).

But according to this https://mockk.io/#property-backing-fields the below code should work, but still fails:

@Test
fun `Just a test`() {
    // mockkConstructor(Boolean::class) -> io.mockk.MockKException: Can't instantiate proxy for class kotlin.Boolean
    val t = spyk(Testeando(), recordPrivateCalls = true)
    every { t getProperty "testBool" } propertyType Boolean::class answers { true }
    // t.testBool = true -> only this works
    assertTrue(t.test())
}

The only way it worked was using t.testBool = true, but if it would be a private property, then it won't work

TomasZ.
  • 449
  • 1
  • 5
  • 10