2 回答
TA贡献1827条经验 获得超4个赞
我们可以在这里使用thenAnswer(),并检查我们的参数
when(this.recalculateInMemoryBoardUtils.fillSetByMarginOrdersUsingFunctions(eq(instancesByUsername), eq(currencyBase), any(), any())).thenAnswer( (Answer<Set<String>>) invocationOnMock -> {
Function<CurrencyPairDTO, String> function = invocationOnMock.getArgument(2);
CurrencyPairDTO currencyPairFunction = CurrencyPairDTO.builder()
.base(currencyBase)
.counter(currencyCounter)
.build();
String currency = function.apply(currencyPairFunction);
if (currencyBase.equals(currency)) {
return ordersBuy;
} else {
return ordersSell;
}
});
TA贡献1874条经验 获得超12个赞
Mockito.thenReturn()通过使用可变参数支持连续调用。因此,您可以将它们结合起来:
ArgumentCaptor<Function<CurrencyPairDTO, String >> currencyPairCaptor = ArgumentCaptor.forClass(Function.class);
ArgumentCaptor<Function<MyOrdersSmartDTO, Set<String>>> myOrderSmartCaptor = ArgumentCaptor.forClass(Function.class);
when(recalculateInMemoryBoardUtils.fillSetByMarginOrdersUsingFunctions(eq(instancesByUsername), eq(currencyBase), currencyPairCaptor.capture(), myOrderSmartCaptor.capture())).thenReturn(ordersSell, ordersBuy);
然后使用getAllValues().get(0)并getAllValues().get(1)像你建议的那样。
此外,返回一个空值而不是模拟它可能更好Set,因为模拟它会使以后的过程更加困难。例如,如果您在测试中调用方法,则someSet.contains(someVal)必须模拟一个基本Set操作才能使测试正常运行。
添加回答
举报