0

The issue is I have two argumentCaptors, and i need to use Mockito.when().then() two times, with this argumentCaptors.capture() in paramenets of method inside when(). But It run two times second argumentCaptor.capture()

i know that in verify i can use argumentCaptor.getAllValues().get(i), and get any of values of current argumentCaptors, but i can not find something about how to use the same thing for capture() method, inside 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);

i received two times ordersBuy instead of ordersSell, ordersBuy

2 Answers 2

1

we can use here thenAnswer(), and check our parameters

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; } });

0

Mockito.thenReturn() supports consecutive calls by using a vararg. Therefore, you can combine them:

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);

And then use getAllValues().get(0) and getAllValues().get(1) like you suggested.

Furthermore, it might be better to return an empty Set instead of mocking it, as mocking it makes the process more difficult later on. For instance, if you method under test invokes someSet.contains(someVal), you'll have to mock a basic Set operation in order for the test to function.

1
  • TY, but its a bit not that i want. I've got an answer now Commented May 24, 2019 at 8:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.