Rolintocour
Rolintocour

Reputation: 3168

kotlin / mockito: Cannot mock a method retuning a boolean

I have issue when trying to mock a method

Example (with mockito-kotlin 5.2.1, mockito-core 5.11.0, kotlin 1.9.23, kotest-assertions-core 5.6.2)

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

Any idea how I could bypass it? Don't want to remove some unit tests because of it...

Upvotes: 0

Views: 77

Answers (1)

Rolintocour
Rolintocour

Reputation: 3168

This is fixed after upgrading to kotlin 2.0

Upvotes: 0

Related Questions