How can I mock Instant.now() using mockK?
This code throws error:
@Test
fun testStaticMock() {
val instant = Instant.parse("2024-03-22T12:00:00.000Z")
mockkStatic(Instant::class) {
every { Instant.now() } returns instant
val actual = Instant.now()
assertEquals(instant, actual)
}
}
Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock
Not really answering your question here, but that's because I think it is bad practice to mock an
Instant. You should instead use aClockand pass it as an argument to your service/function. You can callInstant.now(clock)to get an instant from theClock. AClockmakes your code much more testable, because you can control it better from your tests.In your production code, you can use a real clock with
Clock.system(ZoneId zone),Clock.systemDefault()orClock.systemUTC().In your tests, you can create a fixed-clock with
Clock.fixed(Instant fixedInstant, ZoneId zone), and even make it tick withClock.tick,Clock.tickMinutesorClock.tickSeconds. This will make your tests more predictable, because they won't depend on the time they are run.