kotest-extensions / io.kotest.extensions.time / withConstantNow

withConstantNow

inline fun <T, reified Time : Temporal> withConstantNow(now: Time, block: () -> T): T

Simulate the value of now() while executing block

This function will simulate the value returned by the static method now as a helper to code that is sensitive to now. After the execution of block, the default behavior will be returned.

Supported classes are all classes in the java.time package that supports now, such as LocalDate.now() and LocalDateTime.now(). Less used classes such as JapaneseDate.now() or Year.now() are also supported.

To simulate the value, this function uses Mockk to modify the value returned from the static now function

ATTENTION: This code is very sensitive to race conditions. as the static method is global to the whole JVM instance, if you're mocking now while running in parallel, the results may be inconsistent.