Created
February 14, 2019 16:29
-
-
Save nieldw/cb06e5484bd856d60dcfb992ef545bf8 to your computer and use it in GitHub Desktop.
Use MockK to fix the system clock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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