gpt4 book ai didi

java - 两次第二次 ArgumentCaptor.capture() in Mockito.when()

转载 作者:行者123 更新时间:2023-11-28 21:37:52 27 4
gpt4 key购买 nike

问题是我有两个 argumentCaptors,我需要使用 Mockito.when().then() 两次,这个 argumentCaptors.capture()when() 中方法的参数。但它第二次运行两次 argumentCaptor.capture()

我知道在验证中我可以使用 argumentCaptor.getAllValues().get(i),并获取当前 argumentCaptors 的任何值,但我找不到关于如何使用相同的东西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;
}
});

关于java - 两次第二次 ArgumentCaptor.capture() in Mockito.when(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56288593/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com