【问题标题】:Android Kotlin Testing: Wanted but not invoked, Actually, there were zero interactions with this mockAndroid Kotlin 测试:需要但未调用,实际上,与此模拟的交互为零
【发布时间】:2018-09-13 15:55:33
【问题描述】:

只是想问一下我的 Unit.Testing 错误,所以我有 Unit.Testing 如下所示,当我运行此测试时,我收到错误 需要但未调用:实际上,与此模拟的交互为零。 关键是我想测试我从 api 请求的数据是否成功显示。

在gradle中,我已经实现了

  • org.mockito:mockito-core:2.21.0
  • org.mockito:mockito-inline:2.21.0

我的UnitTesting是这样的,我用mockito做测试:

import com.google.gson.Gson
import com.panritech.fuad.footballmatchapp.TestContextProvider
import com.panritech.fuad.footballmatchapp.api.ApiRepository
import com.panritech.fuad.footballmatchapp.api.TheSportDBApi
import com.panritech.fuad.footballmatchapp.model.MatchItem
import com.panritech.fuad.footballmatchapp.model.MatchItemResponse
import com.panritech.fuad.footballmatchapp.view.MatchView
import org.junit.Test
import org.junit.Before
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations

class MatchPresenterTest {

    @Mock
    private lateinit var matchView: MatchView

    @Mock
    private lateinit var gson: Gson

    @Mock
    private lateinit var apiRepository: ApiRepository

    @Mock
    private lateinit var theSportDBApi: TheSportDBApi

    private lateinit var presenter: MatchPresenter

    @Before
    fun setUp(){
        MockitoAnnotations.initMocks(this)
        presenter = MatchPresenter(matchView,apiRepository, gson, TestContextProvider())
    }


    @Test
    fun testGetMatchList() {
        val match: MutableList<MatchItem> = mutableListOf()
        val response = MatchItemResponse(match)
        val league = "4328"

        `when`(gson.fromJson(apiRepository
                .doRequest(theSportDBApi.getMatch(league))
                ,MatchItemResponse::class.java)).thenReturn(response)

        presenter.getMatchList(league)

        verify(matchView).showMatchList(match)
    }
}

错误详情如下:

Wanted but not invoked:
matchView.showMatchList([]);
-> at com.panritech.fuad.footballmatchapp.presenter.MatchPresenterTest.testGetMatchList(MatchPresenterTest.kt:52)
Actually, there were zero interactions with this mock.

Wanted but not invoked:
matchView.showMatchList([]);
-> at com.panritech.fuad.footballmatchapp.presenter.MatchPresenterTest.testGetMatchList(MatchPresenterTest.kt:52)
Actually, there were zero interactions with this mock.

    at com.panritech.fuad.footballmatchapp.presenter.MatchPresenterTest.testGetMatchList(MatchPresenterTest.kt:52)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

这是我的演示者

class MatchPresenter(private val matchView: MatchView,
                     private val apiRepository: ApiRepository,
                     private val gson: Gson,
                     private val context: CoroutineContextProvider = CoroutineContextProvider()) {
    fun getMatchList(league: String?) {

        async(context.main){
            val data = bg {
                gson.fromJson(apiRepository
                        .doRequest(TheSportDBApi.getMatch(league))
                        , MatchItemResponse::class.java
                )
            }
            matchView.showMatchList(data.await().events)
        }
    }
}

【问题讨论】:

  • 如果您需要更多关于代码的详细信息,您可以在我的 github 中看到它link
  • 您正在使用async ,因此在测试完成之前不会调用协程。我应该说我现在只使用 RxJava,所以我没有使用协程
  • 快速谷歌搜索我发现当你调用presenter.getMatchList(league)时你应该在测试中使用runBlocking
  • 仍然出现同样的错误。我试图打印列表 match 但结果为空
  • 我得到了解决方案,只需要添加launch{ verify(matchView).showMatchList(match) }。谢谢@kingston,你给了我灵感来找到这个解决方案,真的“非常感谢你

标签: android unit-testing kotlin


【解决方案1】:

就我而言,我能够独立执行测试,但是当我尝试执行整个文件时,一些测试会失败。 OP 在评论中添加的解决方案对我有用。我们只需要将verify 函数放入launch

TestCoroutineScope().launch {
            verify(useCase, times(1)).getDetails()
        }
猜你喜欢
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多