Forgive me if this is not the right channel.
I am trying to mock some lazy package level property using Mockk.
package com.aaa.bbb
internal val isEnabled by lazy {
Config.find("enabled") // boolean
}
// use isEnabled
fun featureName() {
return if (isEnabled) {
"feature-name"
} else {
"nothing"
}
}
Then in my test i have the following:
// This would pass
@Test
fun `test enabled case`() {
mockkStatic(Config::class)
every { Config.find("enabled") } returns true
assertEquals(true, Config.find("enabled"))
assertEquals("feature-name", featureName())
}
// This would fail cause isEnabled is cached true
@Test
fun `test enabled case`() {
mockkStatic(Config::class)
every { Config.find("enabled") } returns false
assertEquals(false, Config.find("enabled")) // This is good
assertEquals("nothing", featureName()) // but not here
}
Things I’ve tried (but doesn’t help):
- Separating the above two tests methods into two test classes.
- Using
TestInstance.Lifecycle.PER_METHOD
attribute for the test class.
Can anyone shine some lights on how should I approach this? Is there a way to clear the previously inited lazy val? (which i don’t think it’s possible because it’s val).
Thanks.