Andreas Unterweger
09/22/2019, 8:07 PMclass Test {
@test
internal fun name() {
val mock = mockk<Logic>()
every { mock.doSth() } returns 2
mock.doSth() shouldBe 2
val obj = object : HasLogic {}
obj.doSth() shouldBe 2
}
}
interface HasLogic {
fun doSth() = Logic.doSth()
}
object Logic {
fun doSth() = 1
}wasyl
09/22/2019, 8:10 PMAndreas Unterweger
09/22/2019, 8:10 PMwasyl
09/22/2019, 8:11 PMLogic by doing val mock = mockk<Logic>() it’s only mock variable which, well, is a mock. mockk<Logic>() does not change the object Logic that you defineAndreas Unterweger
09/22/2019, 8:11 PMwasyl
09/22/2019, 8:12 PMHasLogic interface uses Logic object directly, and not your mock, it uses real implementationAndreas Unterweger
09/22/2019, 8:12 PMwasyl
09/22/2019, 8:14 PMim coming from jmockit and there this would workThe difference is
Logic.doSth is not a static method call — it’s a method declared on a Logic singleton instance. If you really need and want static methods, you should drop object Logic declaration whatsoever. Top-level functions are static jvm methodswasyl
09/22/2019, 8:15 PM@JvmStatic would works as well, but I wouldn’t know. Either way you wouldn’t me mocking the entire Logic object, rather each function separately, I guess? Not really sure how to help here, since generally what you should do for testability is to inject the dependencies into the objects you’re testingAndreas Unterweger
09/22/2019, 8:48 PMAndreas Unterweger
09/22/2019, 8:53 PMclass Test {
@Test
internal fun name() {
mockkObject(Logic)
every { Logic.doSth() } returns 2
val obj = object : HasLogic {}
obj.doSth() shouldBe 2
}
}
interface HasLogic {
fun doSth() = Logic.doSth()
}
object Logic {
fun doSth() = 1
}wasyl
09/22/2019, 9:15 PMval mock = mockkObject(Logic)?wasyl
09/22/2019, 9:15 PMmockk library works like that. I’m not using so I wasn’t very helpful 😐Andreas Unterweger
09/22/2019, 10:52 PMJames Richardson
09/23/2019, 6:58 AMJames Richardson
09/23/2019, 7:00 AMAndreas Unterweger
09/23/2019, 10:19 AMLogic.doSth() is an expensive operation, which is tested in its own unit test, i think it can make sense to only verify its called to save runtime