2017-04-12 68 views
-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(); 
    } 
} 
+3

错误表示您的测试未通过,因为Mockito.verify中的方法未被调用。如果你在'ComicListFragment'和'ComicListFragmentPresenter'中发布代码,我们可以试着帮你 –

+1

那么你期望调用'createComicListFromServerResponse'?你试图实际测试*的代码在哪里? (目前,您的测试方法看起来像是在嘲笑一切......) –

+0

将您的演示者的代码放在这里 –

回答

1

当然!

您刺伤了一些调用 - 描述了它在某些情况下的行为。并立即验证是否引用了某些内容。但是你在测试中没有运行任何方法。所以这就是为什么与你的模拟零交互。

你也没有在你的测试中实例化任何真实的类。