You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 20:39:21