我只是想问问我的单位。测试错误,所以我有单元。像下面这样的测试,当我运行这个测试时,我得到了想要的错误,但没有被调用:实际上,这个模拟没有任何交互。重点是我想测试我从api请求的数据是否成功显示。
在gradle中,我已经实现了
我的单元测试是这样的,我用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)
}
}
}
在我的例子中,我能够独立地执行测试,但是当我试图执行整个文件时,一些测试失败了。OP在评论中添加的解决方案对我有效。我们刚刚不得不将验证
函数放入启动
TestCoroutineScope().launch {
verify(useCase, times(1)).getDetails()
}
MyDriveRepository.getMyDriveItemSelectedPathuri一直返回null。我试着查看以下链接,但没有找到解决问题的方法。
一开始我想对我的英语感到抱歉。 这是我测试后得到的: 有人能告诉我我做错了什么吗?
我尝试了例外情况下给出的解决方案:mockito想要但没有调用,实际上与这个mock没有任何交互,而这个mockito也想要但没有调用:实际上,与这个mock没有任何交互但仍然得到相同的错误。我是不是漏掉了什么?以下是me的实现:-
我有接口 接口的实现是 我还有一节课 我正在尝试为MyClass编写JUnit。我已经做了 但我需要mockito但没有调用,实际上在验证调用时与这个mock没有任何交互。 谁能提出一些解决方案。
更新下面是异常消息: 更新2用真实实例替换mocked WithDefinitions对象后,我得到以下输出: