Mocked suspend function returns null in Mockito

Mockito has a special support for suspend functions, but in Kotlin 1.3 there were some changes in how coroutines are implemented internally, so older versions of Mockito are no longer recognize suspend methods compiled by Kotlin 1.3. And kotlinx.coroutines use Kotlin 1.3 since version 1.0.0.

Corresponding support was added to Mockito, but only since version 2.23, so updating your Mockito version will help.


first get Mockito-kotlin and in mockito you can use this code when you want to mock suspend functions :

        val mockedObject: TestClass = mock()
        mockedObject.stub {
            onBlocking { suspendFunction() }.doReturn(true)
        }