kotest-extensions / io.kotest.extensions.time / ConstantNowTestListener

ConstantNowTestListener

class ConstantNowTestListener<Time : Temporal> : ConstantNowListener<Time>, TestListener

Simulate the value of now() while executing a test

This listener will simulate the value returned by the static method now as a helper to code that is sensitive to now. After the execution of the test, 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 listener 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.

Constructors

<init>

Simulate the value of now() while executing a test

ConstantNowTestListener(now: Time)

Functions

afterTest

suspend fun afterTest(testCase: TestCase, result: TestResult): Unit

beforeTest

suspend fun beforeTest(testCase: TestCase): Unit