ArgumentCaptor

Can do everything argument matcher can and still more

You can also get value of parameters of mocked methods, you can then Assert for their value.

Below is the same example of calculator class.

// example statement
final int addition = calculator.getAddition(2, 3);

// Create captor
ArgumentCaptor<Integer> argumentCaptor = ArgumentCaptor.forClass(Integer.class);

Mockito.when(mckCalculator.getAddition(argumentCaptor.capture(), argumentCaptor.capture())).thenReturn(5);

final int firstNum = argumentCaptor.getValue();
Assert.assertEquals(2, firstNum);

// You can get second num by calling getAllValues()
final int secondNum = argumentCaptor.getAllValues()[1];
Assert.assertEquals(3, secondNum);

Last updated