-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ArgumentCaptor A single capture()
call captures all vararg elements.
#585
Comments
Respectful -1. The current actual behavior is more intuitive and looks better in code IMHO. |
I agree with @szczepiq, the behaviour makes sense today, that's what I would expect from However I think I'm not sure we will change the behavior or introduce a new API, we will see. |
The problem of "a single captor captures all varags" is that you can not ensure that only one argument is passed. Here an example:
|
I don't get the example. I can add assertions on captured values, can I not? |
Sure, but you can't use the capturing matcher to verify the number of arguments passed to the varargs method like e.g. In the following scenario you can't ensure that the method was always called with exactly 2 arguments: mock.vararg("1","2","3");
mock.vararg("4","5");
mock.vararg("6");
verify(mock, times(3)).vararg(captor.capture()); Maybe this works, haven't tested: mock.vararg("1","2","3");
mock.vararg("4","5");
mock.vararg("6");
verify(mock, times(3)).vararg(or(anyString(),captor.capture()),
or(anyString(),captor.capture()))); |
Hmmm, I take your point that this case is not possible to test ATM. Is it a real use case from one of your tests? It seems like an ultra corner case. |
With #2835 this now works: ArgumentCaptor<String[]> captor = ArgumentCaptor.forClass(String[].class);
mock.vararg("1","2","3");
mock.vararg("4","5");
mock.vararg("6");
verify(mock, times(3)).vararg(captor.capture());
// captor.getAllValues() will return List.of(new String[]{"1","2","3"}, new String[]{"4","5"}, new String[]{"6"}) It is also possible to verify only a single value was passed: ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
mock.vararg("1","2","3");
mock.vararg("4","5");
mock.vararg("6");
verify(mock, times(1)).vararg(captor.capture()); // <-- only last call matches. |
BREAKING CHANGE: This changes the default behaviour of the `any()` matcher, argument captors and `MockitoHamcrest` matchers when passed to a varargs parameter. Previously, these matcher would match each element in the varargs parameter, matching any number of elements 0...n. From Mockito v5 onwards, when passed to a varargs parameter, will match invocations where a single value is passed to the varargs parameter. To match any number of values passed to the varargs parameter, pass the type of the varargs parameter to the matcher. For example, given a `String...` varargs parameter, use `any(String[].class)`. Fixes #2836 Fixes #1593 Fixes #585
A single
ArgumentCaptor.capture()
call captures all vararg elements.Expected: One capture() call should only capture one vararg argument
Actual: A single
ArgumentCaptor.capture()
call captures all vararg elements.The text was updated successfully, but these errors were encountered: