T
- type of argumentpublic interface ArgumentMatcher<T>
For non-trivial method arguments used in stubbing or verification, you have following options (in no particular order):
ArgumentMatchers.notNull()
. Some times it is better to have a simple test that works than
a complicated test that seem to work.
ArgumentCaptor
to capture the arguments and perform assertions on their state.
Useful when you need to verify the arguments. Captor is not useful if you need argument matching for stubbing.
Many times, this option leads to clean and readable tests with fine-grained validation of arguments.
ArgumentMatcher
interface
and passing the implementation to the ArgumentMatchers.argThat(org.mockito.ArgumentMatcher<T>)
method.
This option is useful if custom matcher is needed for stubbing and can be reused a lot.
Note that ArgumentMatchers.argThat(org.mockito.ArgumentMatcher<T>)
demonstrates NullPointerException auto-unboxing caveat.
MockitoHamcrest.argThat(org.hamcrest.Matcher)
Useful if you already have a hamcrest matcher. Reuse and win!
Note that MockitoHamcrest.argThat(org.hamcrest.Matcher)
demonstrates NullPointerException auto-unboxing caveat.
ArgumentMatcher
since ArgumentMatcher
is effectively a functional interface. A lambda can be used with the ArgumentMatchers.argThat(org.mockito.ArgumentMatcher<T>)
method.
Implementations of this interface can be used with ArgumentMatchers.argThat(org.mockito.ArgumentMatcher<T>)
method.
Use toString()
method for description of the matcher
- it is printed in verification errors.
class ListOfTwoElements implements ArgumentMatcher<List> {
public boolean matches(List list) {
return list.size() == 2;
}
public String toString() {
//printed in verification errors
return "[list of 2 elements]";
}
}
List mock = mock(List.class);
when(mock.addAll(argThat(new ListOfTwoElements))).thenReturn(true);
mock.addAll(Arrays.asList("one", "two"));
verify(mock).addAll(argThat(new ListOfTwoElements()));
To keep it readable you can extract method, e.g:
verify(mock).addAll(argThat(new ListOfTwoElements()));
//becomes
verify(mock).addAll(listOfTwoElements());
In Java 8 you can treat ArgumentMatcher as a functional interface
and use a lambda, e.g.:
verify(mock).addAll(argThat(list -> list.size() == 2));
Read more about other matchers in javadoc for Matchers
class.
ArgumentMatcher
will no longer compile.
All locations where hamcrest matchers are passed to argThat()
will no longer compile.
There are 2 approaches to fix the problems:
describeTo()
method into toString()
method.
org.mockito.hamcrest.MockitoHamcrest.argThat()
instead of Mockito.argThat()
.
Ensure that there is hamcrest dependency on classpath
(Mockito does not depend on hamcrest any more).
Modifier and Type | Method and Description |
---|---|
boolean |
matches(T argument)
Informs if this matcher accepts the given argument.
|
boolean matches(T argument)
The method should never assert if the argument doesn't match. It should only return false.
See the example in the top level javadoc for ArgumentMatcher
argument
- the argument