mockito-kotlin
mockito-kotlin copied to clipboard
Stubbing mocked suspend function returns null
trafficstars
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
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
}