The JVM provides the java.time.Clock interface which is used to generate
Instants. When we have code that relies on time,
we can use a
Clock to generate the values, rather than using things like
Then in tests we can provide a fixed or controllable clock which avoids issues where the time changes on each test run. In your real code, you provide an instance of Clock.systemUTC() or whatever.
The following module is needed:
io.kotest.extensions:kotest-extensions-clock in your build. Search maven central for latest version here.
In order to use it, we create an instance of the
TestClock passing in an instant and a zone offset.
val timestamp = Instant.ofEpochMilli(1234)
val clock = TestClock(timestamp, ZoneOffset.UTC)
We can control the clock via
minus which accept durations, eg
Note that the clock is mutable, and the internal state is changed when you use plus or minus.