Best Kotest code snippet using io.kotest.extensions.time.MutableClock.instant
MutableClockTest.kt
Source:MutableClockTest.kt
...7import java.time.Instant8import java.time.ZoneId9class MutableClockTest : StringSpec() {10 private val zoneId = ZoneId.of("Europe/Warsaw")11 private val instantNow = Instant.now()12 private val clock = MutableClock(instantNow, zoneId)13 init {14 isolationMode = IsolationMode.InstancePerLeaf15 "Set instant for the future" {16 val modifiedClock = clock.withInstant(instantNow.plusSeconds(123))17 modifiedClock.zone shouldBe zoneId18 modifiedClock.millis() shouldNotBe instantNow.toEpochMilli()19 }20 "Set instant for the past" {21 val modifiedClock = clock.withInstant(instantNow.minusSeconds(123))22 modifiedClock.zone shouldBe zoneId23 modifiedClock.millis() shouldNotBe instantNow.toEpochMilli()24 }25 "Change time zone" {26 val modifiedClock = clock.withZone(ZoneId.of("Europe/Paris"))27 modifiedClock.zone shouldNotBe zoneId28 modifiedClock.millis() shouldBe instantNow.toEpochMilli()29 }30 }31}...
MutableClock.kt
Source:MutableClock.kt
...3import java.time.Clock4import java.time.Instant5import java.time.ZoneId6class MutableClock(7 private var instant: Instant,8 private var zone: ZoneId,9) : Clock(), Serializable {10 fun withInstant(instant: Instant): Clock = apply { this.instant = instant }11 override fun withZone(zone: ZoneId): Clock = apply { this.zone = zone }12 override fun getZone(): ZoneId = zone13 override fun instant(): Instant = instant14 override fun millis(): Long = instant.toEpochMilli()15 override fun equals(other: Any?): Boolean {16 if (other == null || other !is MutableClock) return false17 return instant == other.instant && zone == other.zone18 }19 override fun hashCode(): Int = instant.hashCode().xor(zone.hashCode())20 override fun toString(): String = "MutableClock[$instant,$zone]"21}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!