2017-04-12 4 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) 

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(); 
    } 
} 
+3

エラーは、Mockito.verifyのメソッドが呼び出されていないため、テストがパスしていないことを示しています。あなたが 'ComicListFragment'と' ComicListFragmentPresenter'にコードを投稿すると、あなたを助けることができます –

+1

'createComicListFromServerResponse'とは​​何でしょうか?そして実際にテストしようとしているコードはどこにありますか? (現時点では、あなたのテスト方法はすべてを模倣しているように見えます...) –

+0

ここにあなたのプレゼンターのコードを入れてください –

答えて

1

確かに!

いくつかの状況でどのように動作すべきかを説明しました。そして何かが呼び出されたことをすぐに確認する。しかし、テストでは何も実行していませんでした。だからこそ、あなたのモックとのやり取りはゼロになります。

また、実際のクラスはテストでインスタンス化されていません。

関連する問題