android - 无法使用 Mockito 测试使用协程和挂起函数的代码

标签 android unit-testing kotlin mockito kotlinx.coroutines

我正在尝试使用 MVP 编写一个应用程序,并调整我习惯用 Java 编写的异步存储库模式以使用 Kotlin 的挂起函数。但是,我还没有能够正确地测试使用挂起函数的代码。我得到的进一步结果是让测试运行并失败,我认为是模拟没有返回它应该返回的对象,而是返回 null。

这是一个显示问题的小应用程序:

UserRepository(getUser() 方法在返回虚拟用户和抛出异常之间交替):

interface UserRepository {
    suspend fun getUser(): User
}

class UserRepositoryImpl : UserRepository {

    var shouldWork = false

    override suspend fun getUser(): User {

        shouldWork = !shouldWork

        if (shouldWork) {
            return User("Mr. User", "user@example.com")
        } else {
            throw UnableToFetchUserInfoException()
        }

    }
}

View :

interface MainView {
    fun showUserInfo(user: User)
    fun hideUserInfo()
    fun showFailedToLoadUserInfoIndicator()
    fun hideFailedToLoadUserIndicator()
}

class MainActivity : AppCompatActivity(), MainView {

    private lateinit var presenter: MainPresenter

    override fun showUserInfo(user: User) {
        userInfo.text = user.toString()
    }

    override fun hideUserInfo() {
        userInfo.text = ""
    }

    override fun showFailedToLoadUserInfoIndicator() {
        Toast.makeText(this, "Failed to get user info", Toast.LENGTH_SHORT).show()
    }

    override fun hideFailedToLoadUserIndicator() {
        // nothing to do. It's a toast.
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        presenter = MainPresenterImpl(this, UserRepositoryImpl(), UI)

        button.setOnClickListener {
            presenter.onFetchUserButtonClicked()
        }

    }


}

主持人:

interface MainPresenter {
    fun onFetchUserButtonClicked()
}

class MainPresenterImpl(private val view: MainView,
                        private val userRepository: UserRepository,
                        private val coroutineContext: CoroutineContext) : MainPresenter {

    override fun onFetchUserButtonClicked() {
        launch(coroutineContext) {
            view.hideFailedToLoadUserIndicator()
            try {
                val user = userRepository.getUser()
                view.showUserInfo(user)
            } catch (ex: UnableToFetchUserInfoException) {
                view.hideUserInfo()
                view.showFailedToLoadUserInfoIndicator()
            }
        }
    }

}

和测试:

@Test
fun `should show user info on the view when the user request finishes succesfully`() = runBlocking {
    presenter = MainPresenterImpl(viewMock, userRepositoryMock, CommonPool)

    val expectedUser = User("Test", "test@testaing.com")

    whenever(runBlocking { userRepositoryMock.getUser() } ).thenReturn(expectedUser)
    presenter.onFetchUserButtonClicked()
    verify(viewMock, times(1)).showUserInfo(expectedUser)

}

哪些输出...

Argument(s) are different! Wanted:
mainView.showUserInfo(
    User(name=Test, email=test@testaing.com)
);
-> at com.pablobr9.suspendwithtests.MainPresenterImplTest$should show user info on the view when the user request finishes succesfully$1.doResume(MainPresenterImplTest.kt:40)
Actual invocation has different arguments:
mainView.showUserInfo(
    null
);
-> at com.pablobr9.suspendwithtests.MainPresenterImpl$onFetchUserButtonClicked$1.doResume(MainPresenterImpl.kt:15)

我已经尝试了很多东西,但我仍然无法在 Kotlin 中使用挂起函数/协程编写可测试的 MVP 应用程序。甚至不能用这么简单的一个来做到这一点。我在这里缺少什么?

最佳答案

修复已在 mockito 库中,https://github.com/nhaarman/mockito-kotlin/pull/171

验证您的依赖项列表中是否也有 mockito-core 显式,例如:

dependencies {
    // other dependencies...
    // mockito...
    testImplementation "org.mockito:mockito-core:2.13.0"
    // mockito kotlin...
    testImplementation "com.nhaarman:mockito-kotlin:1.5.0"
}

关于android - 无法使用 Mockito 测试使用协程和挂起函数的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48924315/

相关文章:

android 恢复应用程序的默认首选项

java - mock 单例类

java - 如何使用 OpenTok 收听来 self 的应用程序的传入视频通话?

kotlin - 为什么专家将 MutableList 更改为 List?

android - 测试修改后的 Android 源代码的最快方法?

android - 确定 Android 通知的 addAction 点击

c# - 如何在 unity 3d 5 中连接 sqlite 数据库?(无插件)

unit-testing - Ada 有什么好的单元测试框架吗?

unit-testing - 使用 jest 测试 react-native - redux app

Kotlin 1.3 : how to execute a block on a separate thread?