为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

小唯快跑啊 2022-12-15 10:49:47
问题是我有两个 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
查看完整描述

2 回答

?
GCT1015

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;

                    }

                });


查看完整回答
反对 回复 2022-12-15
?
HUWWW

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操作才能使测试正常运行。


查看完整回答
反对 回复 2022-12-15
  • 2 回答
  • 0 关注
  • 88 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信