https://kotlinlang.org logo
#mockk
Title
# mockk
w

wbertan

05/23/2019, 2:07 PM
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

poohbar

05/23/2019, 2:13 PM
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

wbertan

05/23/2019, 2:15 PM
Oh ok... 😞
m

mkobit

05/23/2019, 2:18 PM
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

poohbar

05/23/2019, 2:19 PM
@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

Mike

05/23/2019, 3:32 PM
👍 2
s

sam

05/24/2019, 8:20 AM
That's reason alone to switch to KotlinTest if you're one of the few people not using it 😂
4 Views