用Mockk模拟OffsetDateTime.now

时间:2018-09-28 22:08:57

标签: kotlin mockk

尝试通过模拟来模拟OffsetDateTime.now(),但会抛出

Missing calls inside every { ... } block

我尝试过:

staticMockk<OffsetDateTime>().mock().run {
  val mockTime = OffsetDateTime.now()
  every { OffsetDateTime.now() } returns mockTime
  // test
}

也尝试过:

mockkStatic(OffsetDateTime::class)
val mockTime = OffsetDateTime.now()
every { OffsetDateTime.now() } returns mockTime

1 个答案:

答案 0 :(得分:0)

对我来说还可以。请考虑使用最新版本(1.8.8或1.8.8.kotlin13),或者如果它不起作用,请报告错误。

这是我的代码:

mockkStatic(OffsetDateTime::class)

val date = OffsetDateTime.of(
    2012,
    10,
    10,
    10,
    10,
    10,
    10,
    ZoneOffset.UTC
)

every {
    OffsetDateTime.now()
} returns date

println(OffsetDateTime.now())