亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

兩次第二次 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 關注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號