kotlin mock time code example

Example 1: kotlin mock time

private val now = 1550160535168L
    private val fixedClock = Clock.fixed(Instant.ofEpochMilli(now), ZoneId.systemDefault())

    @BeforeEach
    fun `fix the clock =)`() {
        mockkStatic(Clock::class)
        // Default system clock
        every { Clock.systemUTC() } returns fixedClock
    }

    @Test
    fun `can fix clock`() {
        assertEquals(now, Instant.now().toEpochMilli())
    }

Example 2: kotlin mock time

private val now = 1550160535168L
    private val fixedClock = Clock.fixed(Instant.ofEpochMilli(now), ZoneId.systemDefault())

    @BeforeEach
    fun `fix the clock =)`() {
        mockkStatic(Clock::class)
        // Default system clock
        every { Clock.systemUTC() } returns fixedClock
    }

    @Test
    fun `can fix clock`() {
        assertEquals(now, Instant.now().toEpochMilli())
    }

Example 3: kotlin mock time

private val now = 1550160535168L
    private val fixedClock = Clock.fixed(Instant.ofEpochMilli(now), ZoneId.systemDefault())

    @BeforeEach
    fun `fix the clock =)`() {
        mockkStatic(Clock::class)
        // Default system clock
        every { Clock.systemUTC() } returns fixedClock
    }

    @Test
    fun `can fix clock`() {
        assertEquals(now, Instant.now().toEpochMilli())
    }

Tags:

Misc Example