Ptiseb
Ptiseb

Reputation: 925

Difference on ArgumentCaptor between Mockito v1 and v2

I am currently upgrading the unit tests of a legacy project from Mockito v1 to Mockito v2.

In one of the unit test, I have the following code:

Listener listener = new Listener();
Alert alert = new Alert (listener);
alert.action(); // action calls 5 times the "notify" method on "listener"

ArgumentCaptor<Mess> argument = ArgumentCaptor.forClass(Mess.class);
verify(listener, times(5)).notify(argument.capture());
List<Mess> list_mess = argument.getAllValues(); // A

// The test checks list_mess and does some stuff on "alert"

alert.action(); // action calls 5 times the "notify" method on "listener"

argument = ArgumentCaptor.forClass(Mess.class);
verify(listener, times(10)).notify(argument.capture());
list_mess = argument.getAllValues(); // B

Both "verify" are OK. But since I have been working with Mockito v2, list_mess has a different value:

Does someone have an explanation of this behavior ? I didn't find anything on the Mockito wiki on this point. According to the javadoc of the "capture" method, the list_mess size should still be 10:

Use it to capture the argument. This method must be used inside of verification.

Internally, this method registers a special implementation of an {@link ArgumentMatcher}.

This argument matcher stores the argument value so that you can use it later to perform assertions.

Upvotes: 3

Views: 276

Answers (1)

Ptiseb
Ptiseb

Reputation: 925

Actually it's a known bug by the Mockito team: https://github.com/mockito/mockito/pull/819

Upvotes: 1

Related Questions