Cannot verify varargs parameter as an array
See original GitHub issueCannot verify the varargs by comparing with an array.
@Test
public void shouldVerifyVarargsAsArray() throws Exception {
IMethods mock = mock(IMethods.class);
mock.mixedVarargs("1", "2", "3");
verify(mock).mixedVarargs(any(), eq(new String[] {"2", "3"}));
}
Expected: test runs successfully Actual: the test fails with an argument mismatch error.
org.mockitousage.verification.BasicVerificationTest > shouldVerifyVarargsAsArray FAILED
Argument(s) are different! Wanted:
iMethods.mixedVarargs(<any>, ["2", "3"]);
-> at org.mockitousage.verification.BasicVerificationTest.shouldVerifyVarargsAsArray(BasicVerificationTest.java:125)
Actual invocation has different arguments:
iMethods.mixedVarargs("1", "2", "3");
-> at org.mockitousage.verification.BasicVerificationTest.shouldVerifyVarargsAsArray(BasicVerificationTest.java:123)
...
This was found while upgrading from 1.9.5. In that version it was possible to achieve something similar by creating a custom ArgumentMatcher that implemented VarargMatcher. In that case the ArgumentMatcher was given the varargs as an array. However, in the latest version the same matcher is passed each vararg separately which prevents it from matching.
It is not possible to inline the array as follows because in the actual code the contents of the array are passed as an array into the method that does the verification.
verify(mock).mixedVarargs(any(), eq("1"), eq("2"));
I tried doing something like this:
verify(mock).mixedVarargs(any(), inline(new String[] {"1", "2"}));
}
public static <T> T inline(T[] array) {
for (Object o : array) {
eq(o);
}
return null;
}
But that failed in MatchersBinder because the number of matchers provided (3) did not match the number of arguments passed in the invocation (2).
I also tried to remove the VarargMatcher from the custom ArgumentMatcher but that failed too as the number of arguments did not match in MatcherApplicationStrategy.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:9 (7 by maintainers)
Top GitHub Comments
This is still an issue.
It looks like https://github.com/mockito/mockito/pull/1461/files still fixes it, and can just be included as needed in test code. But it would be a nice to have.