if one has a method such as
Long foo(String... s)
that you wish to mock, you can do
Mockito.when(myMock.foo(ArgumentMatchers.any(String[].class))).thenReturn(Long.valueOf(1));
and if a call occurs such as
myMock.foo();
myMock.foo("hello");
myMock.foo("hello", "there");
it will work fine.
However if a call is made like
myMock.foo((String[]) null);
if fails, as the definition need be nullable(String[].class);
However, if you define it as nullable(String[].class); then
myMock.foo("Hello");
will fail to match as it thinks the type of the matcher is Object, as it calculates that based on the or(String[].class, isNull()) definition.
So it doesn't appear that all cases can be handled by the same syntax. Or am i wrong?
You can easily adapt
InstanceOfmatcher to match null parameters.Alternatively, you can use
ormatcher.Please be aware that Mockito took the opposite direction: in 2.1.0 they changed
any()not to accept null values.Alternative matcher
Note that alternative matcher is almost equivalent to the one used by
nullable(String[].class), but usesisNull(String[].class)instead ofisNull()Matchcer used by
nullable(String[].class)Test: