|
||||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
java.lang.Objectext.test4j.hamcrest.BaseMatcher<T>
org.test4j.hamcrest.matcher.mockito.ArgumentMatcher<T>
T - type of argumentpublic abstract class ArgumentMatcher<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 method and pass an instance of hamcrest Matcher, 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()));
To keep it readable you may want to extract method, e.g:
verify(mock).addAll(argThat(new IsListOfTwoElements())); //becomes verify(mock).addAll(listOfTwoElements());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.
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
| 构造方法摘要 | |
|---|---|
ArgumentMatcher()
|
|
| 方法摘要 | |
|---|---|
void |
describeTo(Description description)
|
abstract boolean |
matches(Object argument)
Returns whether this matcher accepts the given argument. |
| 从类 ext.test4j.hamcrest.BaseMatcher 继承的方法 |
|---|
_dont_implement_Matcher___instead_extend_BaseMatcher_, describeMismatch, toString |
| 从类 java.lang.Object 继承的方法 |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| 构造方法详细信息 |
|---|
public ArgumentMatcher()
| 方法详细信息 |
|---|
public abstract boolean matches(Object argument)
The method should never assert if the argument doesn't match. It should only return false.
argument - the argument
public void describeTo(Description description)
|
||||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||