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操作才能使測試正常運行。
添加回答
舉報