public class Matchers extends Object
AdditionalMatchers
.
Mockito
extends Matchers so to get access to all matchers just import Mockito class statically.
//stubbing using anyInt() argument matcher when(mockedList.get(anyInt())).thenReturn("element"); //following prints "element" System.out.println(mockedList.get(999)); //you can also verify using argument matcher verify(mockedList).get(anyInt());Scroll down to see all methods - full list of matchers.
Warning:
If you are using argument matchers, all arguments have to be provided by matchers.
E.g: (example shows verification but the same applies to stubbing):
verify(mock).someMethod(anyInt(), anyString(), eq("third argument")); //above is correct - eq() is also an argument matcher verify(mock).someMethod(anyInt(), anyString(), "third argument"); //above is incorrect - exception will be thrown because third argument is given without argument matcher.
argThat(org.hamcrest.Matcher<T>)
method and pass an instance of hamcrest Matcher
.
Before you start implementing your own custom argument matcher, make sure you check out ArgumentCaptor
api.
So, how to implement your own argument matcher?
First, you might want to subclass ArgumentMatcher
which is an hamcrest matcher with predefined describeTo() method.
Default description generated by describeTo() uses decamelized class name - to promote meaningful class names.
Example:
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.
Constructor and Description |
---|
Matchers() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
any()
any object or null
|
static <T> T |
any(Class<T> clazz)
any kind object, not necessary of the given class.
|
static boolean |
anyBoolean()
any boolean, Boolean or null.
|
static byte |
anyByte()
any byte, Byte or null
|
static char |
anyChar()
any char, Character or null.
|
static Collection |
anyCollection()
any Collection or null.
|
static <T> Collection<T> |
anyCollectionOf(Class<T> clazz)
generic friendly alias to
anyCollection() . |
static double |
anyDouble()
any double, Double or null.
|
static float |
anyFloat()
any float, Float or null.
|
static int |
anyInt()
any int, Integer or null.
|
static List |
anyList()
any List or null.
|
static <T> List<T> |
anyListOf(Class<T> clazz)
generic friendly alias to
anyList() . |
static long |
anyLong()
any long, Long or null.
|
static Map |
anyMap()
any Map or null.
|
static <T> T |
anyObject()
any Object or null.
|
static Set |
anySet()
any Set or null
|
static <T> Set<T> |
anySetOf(Class<T> clazz)
generic friendly alias to
anySet() . |
static short |
anyShort()
any short, Short or null.
|
static String |
anyString()
any String or null.
|
static <T> T |
anyVararg()
Any vararg, meaning any number and values of arguments.
|
static <T> T |
argThat(Matcher<T> matcher)
Allows creating custom argument matchers.
|
static boolean |
booleanThat(Matcher<Boolean> matcher)
Allows creating custom argument matchers.
|
static byte |
byteThat(Matcher<Byte> matcher)
Allows creating custom argument matchers.
|
static char |
charThat(Matcher<Character> matcher)
Allows creating custom argument matchers.
|
static String |
contains(String substring)
String argument that contains the given substring.
|
static double |
doubleThat(Matcher<Double> matcher)
Allows creating custom argument matchers.
|
static String |
endsWith(String suffix)
String argument that ends with the given suffix.
|
static boolean |
eq(boolean value)
boolean argument that is equal to the given value.
|
static byte |
eq(byte value)
byte argument that is equal to the given value.
|
static char |
eq(char value)
char argument that is equal to the given value.
|
static double |
eq(double value)
double argument that is equal to the given value.
|
static float |
eq(float value)
float argument that is equal to the given value.
|
static int |
eq(int value)
int argument that is equal to the given value.
|
static long |
eq(long value)
long argument that is equal to the given value.
|
static short |
eq(short value)
short argument that is equal to the given value.
|
static <T> T |
eq(T value)
Object argument that is equal to the given value.
|
static float |
floatThat(Matcher<Float> matcher)
Allows creating custom argument matchers.
|
static int |
intThat(Matcher<Integer> matcher)
Allows creating custom argument matchers.
|
static <T> T |
isA(Class<T> clazz)
Object argument that implements the given class.
|
static Object |
isNotNull()
not null argument.
|
static Object |
isNull()
null argument.
|
static long |
longThat(Matcher<Long> matcher)
Allows creating custom argument matchers.
|
static String |
matches(String regex)
String argument that matches the given regular expression.
|
static Object |
notNull()
not null argument.
|
static <T> T |
refEq(T value,
String... excludeFields)
Object argument that is reflection-equal to the given value with support for excluding
selected fields from a class.
|
static <T> T |
same(T value)
Object argument that is the same as the given value.
|
static short |
shortThat(Matcher<Short> matcher)
Allows creating custom argument matchers.
|
static String |
startsWith(String prefix)
String argument that starts with the given prefix.
|
public static boolean anyBoolean()
See examples in javadoc for Matchers
class
false
.public static byte anyByte()
See examples in javadoc for Matchers
class
0
.public static char anyChar()
See examples in javadoc for Matchers
class
0
.public static int anyInt()
See examples in javadoc for Matchers
class
0
.public static long anyLong()
See examples in javadoc for Matchers
class
0
.public static float anyFloat()
See examples in javadoc for Matchers
class
0
.public static double anyDouble()
See examples in javadoc for Matchers
class
0
.public static short anyShort()
See examples in javadoc for Matchers
class
0
.public static <T> T anyObject()
Has aliases: any()
and any(Class clazz)
See examples in javadoc for Matchers
class
null
.public static <T> T anyVararg()
Example:
//verification: mock.foo(1, 2); mock.foo(1, 2, 3, 4); verify(mock, times(2)).foo(anyVararg()); //stubbing: when(mock.foo(anyVararg()).thenReturn(100); //prints 100 System.out.println(mock.foo(1, 2)); //also prints 100 System.out.println(mock.foo(1, 2, 3, 4));See examples in javadoc for
Matchers
classnull
.public static <T> T any(Class<T> clazz)
Sometimes looks better than anyObject() - especially when explicit casting is required
Alias to anyObject()
See examples in javadoc for Matchers
class
null
.public static <T> T any()
Shorter alias to anyObject()
See examples in javadoc for Matchers
class
null
.public static String anyString()
See examples in javadoc for Matchers
class
public static List anyList()
See examples in javadoc for Matchers
class
public static <T> List<T> anyListOf(Class<T> clazz)
anyList()
.
It's an alternative to @SuppressWarnings("unchecked") to keep code clean of compiler warnings.
any List or null.
See examples in javadoc for Matchers
class
public static Set anySet()
See examples in javadoc for Matchers
class
public static <T> Set<T> anySetOf(Class<T> clazz)
anySet()
.
It's an alternative to @SuppressWarnings("unchecked") to keep code clean of compiler warnings.
any Set or null
See examples in javadoc for Matchers
class
public static Map anyMap()
See examples in javadoc for Matchers
class
public static Collection anyCollection()
See examples in javadoc for Matchers
class
public static <T> Collection<T> anyCollectionOf(Class<T> clazz)
anyCollection()
.
It's an alternative to @SuppressWarnings("unchecked") to keep code clean of compiler warnings.
any Collection or null.
See examples in javadoc for Matchers
class
public static <T> T isA(Class<T> clazz)
See examples in javadoc for Matchers
class
T
- the accepted type.clazz
- the class of the accepted type.null
.public static boolean eq(boolean value)
See examples in javadoc for Matchers
class
value
- the given value.0
.public static byte eq(byte value)
See examples in javadoc for Matchers
class
value
- the given value.0
.public static char eq(char value)
See examples in javadoc for Matchers
class
value
- the given value.0
.public static double eq(double value)
See examples in javadoc for Matchers
class
value
- the given value.0
.public static float eq(float value)
See examples in javadoc for Matchers
class
value
- the given value.0
.public static int eq(int value)
See examples in javadoc for Matchers
class
value
- the given value.0
.public static long eq(long value)
See examples in javadoc for Matchers
class
value
- the given value.0
.public static short eq(short value)
See examples in javadoc for Matchers
class
value
- the given value.0
.public static <T> T eq(T value)
See examples in javadoc for Matchers
class
value
- the given value.null
.public static <T> T refEq(T value, String... excludeFields)
This matcher can be used when equals() is not implemented on compared objects. Matcher uses java reflection API to compare fields of wanted and actual object.
Works similarly to EqualsBuilder.reflectionEquals(this, other, exlucdeFields) from apache commons library.
Warning The equality check is shallow!
See examples in javadoc for Matchers
class
value
- the given value.excludeFields
- fields to exclude, if field does not exist it is ignored.null
.public static <T> T same(T value)
See examples in javadoc for Matchers
class
T
- the type of the object, it is passed through to prevent casts.value
- the given value.null
.public static Object isNull()
See examples in javadoc for Matchers
class
null
.public static Object notNull()
alias to isNotNull()
See examples in javadoc for Matchers
class
null
.public static Object isNotNull()
null
.public static String contains(String substring)
See examples in javadoc for Matchers
class
substring
- the substring.public static String matches(String regex)
See examples in javadoc for Matchers
class
regex
- the regular expression.public static String endsWith(String suffix)
See examples in javadoc for Matchers
class
suffix
- the suffix.public static String startsWith(String prefix)
See examples in javadoc for Matchers
class
prefix
- the prefix.public static <T> T argThat(Matcher<T> matcher)
See examples in javadoc for ArgumentMatcher
class
matcher
- decides whether argument matchesnull
.public static char charThat(Matcher<Character> matcher)
See examples in javadoc for Matchers
class
matcher
- decides whether argument matches0
.public static boolean booleanThat(Matcher<Boolean> matcher)
See examples in javadoc for Matchers
class
matcher
- decides whether argument matchesfalse
.public static byte byteThat(Matcher<Byte> matcher)
See examples in javadoc for Matchers
class
matcher
- decides whether argument matches0
.public static short shortThat(Matcher<Short> matcher)
See examples in javadoc for Matchers
class
matcher
- decides whether argument matches0
.public static int intThat(Matcher<Integer> matcher)
See examples in javadoc for Matchers
class
matcher
- decides whether argument matches0
.public static long longThat(Matcher<Long> matcher)
See examples in javadoc for Matchers
class
matcher
- decides whether argument matches0
.public static float floatThat(Matcher<Float> matcher)
See examples in javadoc for Matchers
class
matcher
- decides whether argument matches0
.Copyright © 2018. All rights reserved.