Class ArgumentMatcher<T>
- java.lang.Object
-
- org.hamcrest.BaseMatcher<T>
-
- org.mockito.ArgumentMatcher<T>
-
- Type Parameters:
T
- type of argument
- All Implemented Interfaces:
org.hamcrest.Matcher<T>
,org.hamcrest.SelfDescribing
- Direct Known Subclasses:
And
,Any
,AnyVararg
,CapturingMatcher
,CompareTo
,Contains
,EndsWith
,Equals
,EqualsWithDelta
,Find
,InstanceOf
,Matches
,Not
,NotNull
,Null
,Or
,ReflectionEquals
,Same
,StartsWith
,VarargCapturingMatcher
public abstract class ArgumentMatcher<T> extends org.hamcrest.BaseMatcher<T>
Allows creating customized argument matchers.ArgumentMatcher is an hamcrest
Matcher
with predefined describeTo() method. In case of failure, ArgumentMatcher generates description based on decamelized class name - to promote meaningful class names. For example StringWithStrongLanguage matcher will generate 'String with strong language' description. You can always override describeTo() method and provide detailed description.Use
Matchers.argThat(org.hamcrest.Matcher<T>)
method and pass an instance of hamcrestMatcher
, e.g:
To keep it readable you may want to extract method, e.g:class IsListOfTwoElements extends ArgumentMatcher<List> { public boolean matches(Object list) { return ((List) list).size() == 2; } } List mock = mock(List.class); when(mock.addAll(argThat(new IsListOfTwoElements()))).thenReturn(true); mock.addAll(Arrays.asList("one", "two")); verify(mock).addAll(argThat(new IsListOfTwoElements()));
Warning: Be reasonable with using complicated argument matching, especially custom argument matchers, as it can make the test less readable. Sometimes it's better to implement equals() for arguments that are passed to mocks (Mockito naturally uses equals() for argument matching). This can make the test cleaner.verify(mock).addAll(argThat(new IsListOfTwoElements())); //becomes verify(mock).addAll(listOfTwoElements());
Also, sometimes
ArgumentCaptor
may be a better fit than custom matcher. For example, if custom argument matcher is not likely to be reused or you just need it to assert on argument values to complete verification of behavior.Read more about other matchers in javadoc for
Matchers
class
-
-
Constructor Summary
Constructors Constructor Description ArgumentMatcher()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
describeTo(org.hamcrest.Description description)
By default this method decamelizes matchers name to promote meaningful names for matchers.abstract boolean
matches(java.lang.Object argument)
Returns whether this matcher accepts the given argument.
-
-
-
Method Detail
-
matches
public abstract boolean matches(java.lang.Object argument)
Returns whether this matcher accepts the given argument.The method should never assert if the argument doesn't match. It should only return false.
- Parameters:
argument
- the argument- Returns:
- whether this matcher accepts the given argument.
-
describeTo
public void describeTo(org.hamcrest.Description description)
By default this method decamelizes matchers name to promote meaningful names for matchers.For example StringWithStrongLanguage matcher will generate 'String with strong language' description in case of failure.
You might want to override this method to provide more specific description of the matcher (useful when verification failures are reported).
- Parameters:
description
- the description to which the matcher description is appended.
-
-