I have issue when trying to mock a method
• suspended
• with context
• returning a primitive
Example (with mockito-kotlin 5.2.1, mockito-core 5.11.0, kotlin 1.9.23, kotest-assertions-core 5.6.2)
Copy code
class TheBug {
@Test
fun test(): Unit = runBlocking{
val service = mock<MyService> {
onBlocking {
with(any<MyContext>()) {
myMethod()
}
}.doReturn(true)
}
with(MyContext("toto")) {
service.myMethod() shouldBe true
}
}
}
class MyService {
context(MyContext)
suspend fun myMethod(): Boolean {
return true
}
}
data class MyContext(val string: String)
If the method
• isn't suspended,
• or doesn't require a context
• or returns a data class instead of a boolean
then it works as expected
Any idea how I could bypass it 🙏?
Don't want to remove some unit tests because of it...
o
Oğuzhan Soykan
05/22/2024, 12:44 PM
Have you tried with different mock library, such as mockk? Using kotlin native mocking library might change the result 🤔
l
Laurent Thiebaud
05/22/2024, 12:46 PM
Unfortunately not an option to me, we're already using mockito on the whole project
o
Oğuzhan Soykan
05/22/2024, 3:50 PM
You don’t have to change it in your entire project, It would help investigating further for the root cause
plus1 1
l
Laurent Thiebaud
05/27/2024, 1:07 PM
Actually I upgraded to kotlin 2.0 and it fixes the issue 🎉