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

diego-gomez-olvera

09/22/2021, 12:39 PM
it seems that a recommended way to mock extensions with Mockk is to define a 
@file:JvmName
 and refer to it with 
mockkStatic("<jvm-name>")
, which is not type safe: In case of refactoring (e.g. rename or package change), the tests might break inadvertently. Is there a more type safe manner to mock extension functions?
m

Mattia Tommasone

09/22/2021, 12:41 PM
yes, it’s a relatively recent addition: https://mockk.io/#extension-functions
you can use either
Copy code
mockkStatic(Obj::extensionFunc)
or
Copy code
@file:JvmName("KHttp")
which has indeed the problem that it is not safe towards class renaming, you’re right
👍 1
d

diego-gomez-olvera

09/22/2021, 1:03 PM
ah, I like the alternative using the method reference. It is potentially more code to write (for several extensions), but also more robust
thanks!
👍 1
j

Justin Tullgren

09/24/2021, 6:30 PM
just ran into this, good alternative
3 Views