I have issue when trying to mock a method - suspen...
# server
l
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)
Test result is
java.lang.NullPointerException: Cannot invoke "java.lang.Boolean.booleanValue()"
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
Have you tried with different mock library, such as mockk? Using kotlin native mocking library might change the result 🤔
l
Unfortunately not an option to me, we're already using mockito on the whole project
o
You don’t have to change it in your entire project, It would help investigating further for the root cause
plus1 1
l
Actually I upgraded to kotlin 2.0 and it fixes the issue 🎉
🙌 1