我为我的应用编写了以下单元测试。我刚开始学习UnitTesting& Mockito所以异常/错误有点令人困惑。以下测试引发了一个奇怪的例外。
Mockito错误:
Wanted but not invoked:
mComicListFragmentPresenter.createComicListFromServerResponse(
[com.myapp.comic.models.serverResponse.Result@387a8303, com.myapp.comic.models.serverResponse.Result@28cda624, com.myapp.comic.models.serverResponse.Result@1500b2f3]
);
-> at com.myapp.comic.ComicListFragmentPresenterUnitTest.testForCheckingSuccessBehaviorUponFetchingComicsFromServer(ComicListFragmentPresenterUnitTest.java:47)
Actually, there were zero interactions with this mock.
Wanted but not invoked:
mComicListFragmentPresenter.createComicListFromServerResponse(
[com.myapp.comic.models.serverResponse.Result@387a8303, com.myapp.comic.models.serverResponse.Result@28cda624, com.myapp.comic.models.serverResponse.Result@1500b2f3]
);
-> at com.myapp.comic.ComicListFragmentPresenterUnitTest.testForCheckingSuccessBehaviorUponFetchingComicsFromServer(ComicListFragmentPresenterUnitTest.java:47)
Actually, there were zero interactions with this mock.
at com.myapp.comic.ComicListFragmentPresenterUnitTest.testForCheckingSuccessBehaviorUponFetchingComicsFromServer(ComicListFragmentPresenterUnitTest.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
UnitTest代码:
@RunWith(MockitoJUnitRunner.class)
public class MyUnitTest {
@Mock
ComicListFragment mComicListFragment;
@Mock
ComicListFragmentPresenter mComicListFragmentPresenter;
List<Result> mResultList = Arrays.asList(new Result(), new Result(), new Result());
@Test
public void testForCheckingSuccessBehaviorUponFetchingComicsFromServer() {
doNothing().when(mComicListFragment).onComicsFetchedSuccessfully(mResultList);
Mockito.verify(mComicListFragmentPresenter, times(1)).createComicListFromServerResponse(mResultList);
Mockito.verify(mComicListFragment, times(1)).onComicListCreationComplete();
}
}
答案 0 :(得分:1)
当然!
你刺伤了一些调用 - 描述了它在某些情况下应该如何表现。并立即验证是否调用了某些内容。但是你没有在测试中运行任何方法。这就是为什么与你的模拟零交互。
您也没有在测试中实例化任何真实课程。