mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.1k stars 200 forks source link

Stubbing mocked suspend function returns null #379

Open iadcialim opened 4 years ago

iadcialim commented 4 years ago
fun testHandleCallSuccess() {

    val call = mock<suspend () -> Result<Int>>()
    val callback = Mockito.mock(Callback::class.java) as Callback<Int>
    runBlocking {
        whenever(call.invoke()).thenReturn(Result.success(0))
        baseViewModel.handleCall(call, callback)
    }
    verify(callback).success(0)
}

whenever(call.invoke()).thenReturn(Result.success(0)) will return null! Is this an issue of Mockito or this lib?

SO link: https://stackoverflow.com/q/61097992/8258130

spoptchev commented 2 years ago

I can confirm the issue on functional interfaces.

interface Supply : suspend (Input) -> Output

In tests the stub whenever(supply(input)).thenReturn(output) will return null instead of the specified output.

The issue does not appear when the functional interface is declared manually:

interface Supply {
    suspend operator fun invoke(input: Input): Output
}