模拟的暂停功能在Mockito中返回null [英] Mocked suspend function returns null in Mockito

查看:194
本文介绍了模拟的暂停功能在Mockito中返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Mockito模拟的暂停函数,但是它返回null

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

两个项目都使用

'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0'

示例1

这是我的测试,其中的模拟返回null

here is my test in which the mock is returning null

@Test
fun `when gps not enabled observer is notified`() = runBlocking {
    // arrange
    `when`(suspendingLocationService.getCurrentLocation()).thenReturn(result) // <- when called this returns null

    // act
    presenter.onStartShopButtonClick()

    // assert
    verify(view).observer
    verify(observer).onPrepareShop()
}

我的演示者中具有以下实现

I have the below implementation in my presenter

  override suspend fun onStartShopButtonClick() {
    val result = suspendingLocationService.getCurrentLocation() // <- in my test result is null!!!!!!
    view?.apply {
        observer?.onPrepareShop()
        when {
            result.hasGivenPermission == false -> observer?.onStartShop(StoreData(), APIError(APIError.ErrorType.NO_PERMISSION))
            result.hasGPSEnabled == false -> observer?.onStartShop(StoreData(), APIError(APIError.ErrorType.GPS_NOT_ENABLED))
            result.latitude != null && result.longitude != null ->
                storeLocationService.getCurrentStore(result.latitude, result.longitude) { store, error ->
                    observer?.onStartShop(store, error)
                }
        }
    }
}

无论如何,我相信下面的一种非常相似的实现方式

however I have what I believe to a very similar implementation that is working below

示例2

下面的测试确实通过了,并且该功能正确地响应了产品

The below test does pass and the correct the function does respond with a product

@Test
fun `suspending implementation updates label`() = runBlocking {
    // arrange
    `when`(suspendingProductProvider.getProduct("testString")).thenReturn(product)

    // act
    presenter.textChanged("testString")

    // assert
    verify(view).update(product.name)
}

这是演示者的实现

override suspend fun textChanged(newText: String?) {
    val product = suspendingNetworkProvider.getProduct(newText)
    view?.update(product.name)
}

这是我在嘲笑的界面

interface SuspendingProductProvider {
    suspend fun getProduct(search: String?): Product
}

在第一个示例中我没有做什么

what I am not doing in the first example

推荐答案

Mockito对suspend函数有特殊的支持,但是在Kotlin 1.3中,协程的内部实现方式发生了一些变化,因此Mockito的较旧版本没有不再识别Kotlin 1.3编译的suspend方法.并且kotlinx.coroutines从1.0.0版开始使用Kotlin 1.3.

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.

相应的支持已添加到Mockito,但仅从2.23版开始 ,因此更新您的Mockito版本会有所帮助.

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

这篇关于模拟的暂停功能在Mockito中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆