java - Mockito.when() 内的两次 ArgumentCaptor.capture()
问题描述
问题是我有两个argumentCaptors,我需要使用Mockito.when().then()
两次,这个argumentCaptors.capture()
在方法的参数里面when()
。但它每秒运行两次argumentCaptor.capture()
我知道在验证中我可以使用argumentCaptor.getAllValues().get(i)
,并获取当前参数Captors的任何值,但我找不到关于如何在capture()
方法中使用相同的东西的东西,里面Mockito.when()
Set<String> ordersBuy = mock(Set.class);
Set<String> ordersSell = mock(Set.class);
ArgumentCaptor<Function<CurrencyPairDTO, String >> getBase = ArgumentCaptor.forClass(Function.class);
ArgumentCaptor<Function<CurrencyPairDTO, String>> getCounter = ArgumentCaptor.forClass(Function.class);
ArgumentCaptor<Function<MyOrdersSmartDTO, Set<String>>> getSell = ArgumentCaptor.forClass(Function.class);
ArgumentCaptor<Function<MyOrdersSmartDTO, Set<String>>> getBuy = ArgumentCaptor.forClass(Function.class);
when(this.recalculateInMemoryBoardUtils.fillSetByMarginOrdersUsingFunctions(eq(instancesByUsername), eq(currencyBase), getBase.capture(), getSell.capture())).thenReturn(ordersSell);
when(this.recalculateInMemoryBoardUtils.fillSetByMarginOrdersUsingFunctions(eq(instancesByUsername), eq(currencyBase), getCounter.capture(), getBuy.capture())).thenReturn(ordersBuy);
我收到了两次 ordersBuy 而不是 ordersSell,ordersBuy
解决方案
我们可以在这里使用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;
}
});
推荐阅读
- css - 通过 CSS 缩放图像
- google-chrome - 在容器映像上调用 AWS Lambda 函数时出现量角器错误代码 199
- drupal-8 - Drupal 8.9.17 和 apache 2.4.44 或更高版本
- python - 如何在函数调用链python中共享变量
- java - 为什么客户端在向服务器发送对象时冻结?
- c++ - 关于创建我自己的模板化函数进行排序的问题
- reactjs - TypeError:无法添加属性数量,对象不可扩展
- php - 根据浏览器和视口自动提供最佳图像
- json - 如何快速将 JSON 数据写入/保存到 Xcode 中的 .json 文件中?
- flutter - Flutter:如何修复“多个小部件使用相同的 GlobalKey”错误