私のサービスで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()がコレクションを返すことができないと思っていますが、これを説明するリソースは見つかりませんでした。誰かが何かヒントを知っているなら、私に知らせてください!たくさんのありがとう!
これは機能します。あなたのバージョンがなぜ機能するのか簡単に説明できますか?ありがとう! –
あなたはどのバージョンのmockitoを使用していますか? – want2learn
私は1.10.19を使用していると信じています。 –