You can use `Instant.parse("2017-11-01T11:04:56Z")...
# mockk
w
You can use
Instant.parse("2017-11-01T11:04:56Z")
before the
mockkStatic
.
@poohbar
Copy code
@Test
    fun `test mocking Instant`() {
        val expectedResult = Instant.parse("2017-11-01T11:04:56Z")

        mockkStatic(Instant::class)
        every { Instant.now() } returns expectedResult

        val result = Instant.now()
        assertEquals(expectedResult, result)

        unmockkStatic(Instant::class)
    }
p
yeah, the issue is that i thought i could mock this globally for all the tests
but some of the implementation is using
Instant.parse
and i dont want to mock that.. i jus want to mock
Instant.now
w
Oh ok... 😞
m
IMO it is better to inject a https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html and retrieve time from that instead using
instant()
instead of relying on global state
p
@mkobit yep, I was trying to avoid it because now almost every class will need to have another dependency but I guess there is no other option
m
👍 2
s
That's reason alone to switch to KotlinTest if you're one of the few people not using it 😂