我正在对我的服务之一进行Mockito单元测试,并试图嘲笑它,但无法返回所需的对象。我的代码片段看起来是这样的:Mockito当...然后返回没有返回集合的期望值
@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")
通过测试案例,但countryToMarketplaceMapper.getMarketplacesForCountries(countries)
失败,因为junit.framework.AssertionFailedError: expected:<[]> but was:<[1]>
。
我很困惑,难道我只是模拟countryToMarketplaceMapper.getMarketplacesForCountries(countries)
的行为返回一个marketplaces
哪里有条目?我做了一些研究,发现这篇文章:Mockito when/then not returning expected value,并且我使用“doReturn()... when()”来定义模仿行为的方式,但仍然无法解决此问题。
我想也许是因为然后返回()不能返回的东西的集合,但我没有找到任何资源解释这一点。如果有人知道一些提示,请让我知道!非常感谢!
它的工作原理!你能简单解释一下你的版本为什么可以工谢谢! –
你正在使用哪个版本的mockito? – want2learn
我相信我在使用1.10.19。 –