Compatibility with java.time
Control the virtual time during tests using the java.time
package.
Builds upon the virtual time control available out-of-the-box to allow instancing clocks, setting the current time or waiting for a given time.
Example
We want to test a Java class that makes computations based on the current time.
To allow writing multiple tests using the same class, we declare it as a prepared value. To ensure it has access to the virtual time, we inject the Java clock.
val prepareComputer by prepared {
TimeComputer(
clock = time.clockJava,
)
}
Content copied to clipboard
Now, we can use the helper functions to set the current time and to wait until a specific time.
test("A test that uses the time") {
time.set(Instant.parse("2023-11-08T12:00:00Z"))
val computer = prepareComputer()
computer.isInTheFuture("2023-11-08T11:00:00Z") shouldBe false
}
Content copied to clipboard