Mocked suspend function returns null in Mockito

后端 未结 2 1480
别跟我提以往
别跟我提以往 2021-01-04 14:58

I have a suspending functions that I have mocked, using Mockito but it is returning null

both projects use

\'org.jetbrains.kotlinx:kotlinx-coroutines         


        
相关标签:
2条回答
  • 2021-01-04 15:20

    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.

    0 讨论(0)
  • 2021-01-04 15:20

    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)
            }
    
    0 讨论(0)
提交回复
热议问题