-1
我已经为我的应用程序编写了以下单元测试。我刚开始学习UnitTesting & Mockito,所以异常/错误有点令人困惑。以下测试引发了一个奇怪的例外。Mockito投掷“通缉但未引用”
错误的Mockito:
Wanted but not invoked:
mComicListFragmentPresenter.createComicListFromServerResponse(
[[email protected], [email protected], [email protected]]
);
-> at com.myapp.comic.ComicListFragmentPresenterUnitTest.testForCheckingSuccessBehaviorUponFetchingComicsFromServer(ComicListFragmentPresenterUnitTest.java:47)
Actually, there were zero interactions with this mock.
Wanted but not invoked:
mComicListFragmentPresenter.createComicListFromServerResponse(
[[email protected], [email protected], [email protected]]
);
-> 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)
单元测试代码:
@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();
}
}
错误表示您的测试未通过,因为Mockito.verify中的方法未被调用。如果你在'ComicListFragment'和'ComicListFragmentPresenter'中发布代码,我们可以试着帮你 –
那么你期望调用'createComicListFromServerResponse'?你试图实际测试*的代码在哪里? (目前,您的测试方法看起来像是在嘲笑一切......) –
将您的演示者的代码放在这里 –