Skip to content

Instantly share code, notes, and snippets.

@nieldw
Created February 14, 2019 16:29
Show Gist options
  • Save nieldw/cb06e5484bd856d60dcfb992ef545bf8 to your computer and use it in GitHub Desktop.
Save nieldw/cb06e5484bd856d60dcfb992ef545bf8 to your computer and use it in GitHub Desktop.
Use MockK to fix the system clock
import io.mockk.every
import io.mockk.mockkStatic
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.time.Clock
import java.time.Instant
import java.time.ZoneId
internal class ClockFixingTest {
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())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment