Current Instant Listeners
#
Current instant listenersSometimes you may want to use the now
static functions located in java.time
classes for multiple reasons, such as setting the creation date of an entity
data class MyEntity(creationDate: LocalDateTime = LocalDateTime.now())
.
But what to do when you want to test that value? now
will be different
each time you call it!
For that, Kotest provides ConstantNowListener
and withConstantNow
functions.
While executing your code, your now
will always be the value that you want to test against.
Or, with a listener for all the tests:
caution
withContantNow
and ConstantNowTestListener
are very sensitive to race conditions. Using them, mocks the static method now
which is global to the whole JVM instance,
if you're using it while running test in parallel, the results may be inconsistent.