2017-08-11 19 views
0

私のサービスでMockito単体テストを行っていますが、私はそれをモックしようとしましたが、目的のオブジェクトを返すことができません。私のコードのスニペットは次のようになります。今Mockito When ...コレクションの期待値を返さない返す

@RunWith(MockitoJUnitRunner.class) 
    public class RunBinaryApprovalActivityTest { 

     @Mock 
     CountryToMarketplaceMapper countryToMarketplaceMapper; 

     @Test 
     void doSomeTestHere() { 
     Set<Integer> marketplaces = new HashSet<Integer>(); 
     marketplaces.add(1); 

     List<String> countries = new ArrayList<String>(); 
     countries.add("US"); 




Mockito.when(countryToMarketplaceMapper.getMarketplacesForCountries(Mockito.anyCollection())).thenReturn(marketplaces); 

Mockito.when(otherTestInstance.otherMethod("inputString")).thenReturn("ExpectedOutput"); 

Assert.assertEquals(otherTestInstance.otherMethod("inputString"),"ExpectedOutput"); 

Assert.assertEquals(countryToMarketplaceMapper.getMarketplacesForCountries(countries), marketplaces); 


     } 


    } 

otherTestInstance.otherMethod("inputString")は、テストケースに合格したが、理由はjunit.framework.AssertionFailedError: expected:<[]> but was:<[1]>countryToMarketplaceMapper.getMarketplacesForCountries(countries)に失敗しました。

私は混乱していますが、ちょうどcountryToMarketplaceMapper.getMarketplacesForCountries(countries)の動作をシミュレートしていないのですが、内部にエントリがあるmarketplacesを返しますか?私はいくつかの研究を行い、このポストを見つけました:Mockito when/then not returning expected valueと私は "doReturn()... when()"を使って嘲笑動作をどのように定義したのですか、まだこの問題は解決していません。

私は多分それはthenReturn()がコレクションを返すことができないと思っていますが、これを説明するリソースは見つかりませんでした。誰かが何かヒントを知っているなら、私に知らせてください!たくさんのありがとう!

答えて

0

あなたが使用しているjavaとmockitoのバージョンはわかりません。 試用版

Mockito.when(countryToMarketplaceMapper.getMarketplacesForCountries(Mockito.anyListOf(String.class))).thenReturn(marketplaces); 
+0

これは機能します。あなたのバージョンがなぜ機能するのか簡単に説明できますか?ありがとう! –

+0

あなたはどのバージョンのmockitoを使用していますか? – want2learn

+0

私は1.10.19を使用していると信じています。 –

関連する問題