使用Mockk模拟Boolean属性失效问题排查
Mockk测试Boolean属性的问题解决方法
你的问题出在不必要地使用了mockkConstructor(Boolean::class):
- Boolean是基本数据类型的包装类,属于Mockk无法instrument的类型,所以会抛出"Non instrumentable classes(skipped): boolean"警告。
- 这一步完全冗余,你要mock的是
Testeando类的testBool属性,而非Boolean类的构造逻辑,多余的mock操作反而干扰了Mockk对目标属性的存根,导致你的every{t.testBool }returns true被忽略。
直接删除mockkConstructor(Boolean::class)即可,正确的测试代码如下:
@Test fun `Just a test`() { val t = spyk(Testeando()) every { t.testBool } returns true assertTrue(t.test()) }
内容的提问来源于stack exchange,提问作者Sergio Magoo Quintero
相关产品推荐
相关产品推荐

