I want to do this:
verify(function, Mockito.times(1)).doSomething(argument1, Matchers.any(Argument2.class));
Where argument1 is a specfic instance of type Argument1 and argument2 is any instance of the type Argument2.
But I get an error:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 2 matchers expected, 1 recorded. This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String"); When using matchers, all arguments have to be provided by matchers. For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
Following that advice I can write the following and everything is fine:
verify(function, Mockito.times(1)).doSomething(Matchers.any(Argument1.class), Matchers.any(Argument2.class));
Where I am looking for any arguement of type Argument1 and any argument of type Argument2.
How can I acheive this desired behaviour?
Since Mockito any(Class) and anyInt family matchers perform a type check, thus they won't match null arguments.
org. mockito. Matchers is deprecated, ArgumentMatchers should be used instead.
anyString() does not work for null #185.
Mockito allows us to create mock objects and stub the behavior for our test cases. We usually mock the behavior using when() and thenReturn() on the mock object.
There is more than one possible argument matcher and one is eq
, which is mentioned in the exception message. Use:
verify(function, times(1)).doSomething(eq(arg1), any(Argument2.class));
(static imports supposed to be there -- eq()
is Matchers.eq()
).
You also have same()
(which does reference equality, ie ==
), and more generally you can write your own matchers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With