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.
Matcher methods like anyObject()
, eq()
do not return matchers.
Internally, they record a matcher on a stack and return a dummy value (usually null).
This implementation is due static type safety imposed by java compiler.
The consequence is that you cannot use anyObject()
, eq()
methods outside of verified/stubbed method.
ArgumentMatcher
to learn about approaches and see the examples.Constructor and Description |
---|
Matchers() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
any()
Matches anything, including nulls
|
static <T> T |
any(Class<T> clazz)
Matches any object, including nulls
|
static boolean |
anyBoolean()
Any
boolean or non-null Boolean |
static byte |
anyByte()
Any
byte or non-null Byte . |
static char |
anyChar()
Any
char or non-null Character . |
static Collection |
anyCollection()
Any non-null
Collection . |
static <T> Collection<T> |
anyCollectionOf(Class<T> clazz)
Generic friendly alias to
anyCollection() . |
static double |
anyDouble()
Any
double or non-null Double . |
static float |
anyFloat()
Any
float or non-null Float . |
static int |
anyInt()
Any int or non-null Integer.
|
static List |
anyList()
Any non-null
List . |
static <T> List<T> |
anyListOf(Class<T> clazz)
Generic friendly alias to
anyList() . |
static long |
anyLong()
Any
long or non-null Long . |
static Map |
anyMap()
Any non-null
Map . |
static <K,V> Map<K,V> |
anyMapOf(Class<K> keyClazz,
Class<V> valueClazz)
Generic friendly alias to
anyMap() . |
static <T> T |
anyObject()
Matches anything, including null.
|
static Set |
anySet()
Any non-null
Set . |
static <T> Set<T> |
anySetOf(Class<T> clazz)
Generic friendly alias to
anySet() . |
static short |
anyShort()
Any
short or non-null Short . |
static String |
anyString()
Any non-null
String |
static <T> T |
anyVararg()
Any vararg, meaning any number and values of arguments.
|
static <T> T |
argThat(ArgumentMatcher<T> matcher)
Allows creating custom argument matchers.
|
static boolean |
booleanThat(ArgumentMatcher<Boolean> matcher)
Allows creating custom
boolean argument matchers. |
static byte |
byteThat(ArgumentMatcher<Byte> matcher)
Allows creating custom
byte argument matchers. |
static char |
charThat(ArgumentMatcher<Character> matcher)
Allows creating custom
char argument matchers. |
static String |
contains(String substring)
String argument that contains the given substring. |
static double |
doubleThat(ArgumentMatcher<Double> matcher)
Allows creating custom
double 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(ArgumentMatcher<Float> matcher)
Allows creating custom
float argument matchers. |
static int |
intThat(ArgumentMatcher<Integer> matcher)
Allows creating custom
int argument matchers. |
static <T> T |
isA(Class<T> clazz)
Object argument that implements the given class. |
static Object |
isNotNull()
Not
null argument. |
static <T> T |
isNotNull(Class<T> clazz)
Not
null argument, not necessary of the given class. |
static Object |
isNull()
null argument. |
static <T> T |
isNull(Class<T> clazz)
null argument. |
static long |
longThat(ArgumentMatcher<Long> matcher)
Allows creating custom
long argument matchers. |
static String |
matches(String regex)
String argument that matches the given regular expression. |
static Object |
notNull()
Not
null argument. |
static <T> T |
notNull(Class<T> clazz)
Not
null argument, not necessary of the given class. |
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(ArgumentMatcher<Short> matcher)
Allows creating custom
short argument matchers. |
static String |
startsWith(String prefix)
String argument that starts with the given prefix. |
public static boolean anyBoolean()
false
.public static byte anyByte()
0
.public static char anyChar()
0
.public static int anyInt()
See examples in javadoc for Matchers
class
0
.public static long anyLong()
0
.public static float anyFloat()
0
.public static double anyDouble()
0
.public static short anyShort()
0
.public static <T> T anyObject()
This is an alias of: any()
and any(java.lang.Class)
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)
This method doesn't do type checks with the given parameter, it is only there to avoid casting in your code. This might however change (type checks could be added) in a future major release.
See examples in javadoc for Matchers
class
This is an alias of: any()
and anyObject()
null
.public static <T> T any()
Shorter alias to anyObject()
See examples in javadoc for Matchers
class
This is an alias of: anyObject()
and any(java.lang.Class)
null
.public static String anyString()
String
See examples in javadoc for Matchers
class
public static List anyList()
List
.
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 non-null List
.
This method doesn't do type checks with the given parameter, it is only there to avoid casting in your code. This might however change (type checks could be added) in a future major release.
See examples in javadoc for Matchers
class
clazz
- Type owned by the list to avoid castingpublic static Set anySet()
Set
.
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 non-null Set
.
This method doesn't do type checks with the given parameter, it is only there to avoid casting in your code. This might however change (type checks could be added) in a future major release.
See examples in javadoc for Matchers
class
clazz
- Type owned by the Set to avoid castingpublic static Map anyMap()
Map
.
See examples in javadoc for Matchers
class
public static <K,V> Map<K,V> anyMapOf(Class<K> keyClazz, Class<V> valueClazz)
anyMap()
.
It's an alternative to @SuppressWarnings("unchecked") to keep code clean of compiler warnings.
Any non-null Map
.
This method doesn't do type checks with the given parameter, it is only there to avoid casting in your code. This might however change (type checks could be added) in a future major release.
See examples in javadoc for Matchers
class
keyClazz
- Type of the map key to avoid castingvalueClazz
- Type of the value to avoid castingpublic static Collection anyCollection()
Collection
.
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 non-null Collection
.
This method doesn't do type checks with the given parameter, it is only there to avoid casting in your code. This might however change (type checks could be added) in a future major release.
See examples in javadoc for Matchers
class
clazz
- Type owned by the collection to avoid castingpublic static <T> T isA(Class<T> clazz)
Object
argument that implements the given class.
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)
boolean
argument that is equal to the given value.
See examples in javadoc for Matchers
class
value
- the given value.0
.public static byte eq(byte value)
byte
argument that is equal to the given value.
See examples in javadoc for Matchers
class
value
- the given value.0
.public static char eq(char value)
char
argument that is equal to the given value.
See examples in javadoc for Matchers
class
value
- the given value.0
.public static double eq(double value)
double
argument that is equal to the given value.
See examples in javadoc for Matchers
class
value
- the given value.0
.public static float eq(float value)
float
argument that is equal to the given value.
See examples in javadoc for Matchers
class
value
- the given value.0
.public static int eq(int value)
int
argument that is equal to the given value.
See examples in javadoc for Matchers
class
value
- the given value.0
.public static long eq(long value)
long
argument that is equal to the given value.
See examples in javadoc for Matchers
class
value
- the given value.0
.public static short eq(short value)
short
argument that is equal to the given 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()
null
argument.
See examples in javadoc for Matchers
class
null
.public static <T> T isNull(Class<T> clazz)
null
argument.
The class argument is provided to avoid casting.
See examples in javadoc for Matchers
class
clazz
- Type to avoid castingnull
.public static Object notNull()
null
.public static <T> T notNull(Class<T> clazz)
null
argument, not necessary of the given class.
The class argument is provided to avoid casting.
alias to isNotNull(Class)
See examples in javadoc for Matchers
class
clazz
- Type to avoid castingnull
.public static Object isNotNull()
null
.public static <T> T isNotNull(Class<T> clazz)
null
argument, not necessary of the given class.
The class argument is provided to avoid casting.
alias to notNull(Class)
See examples in javadoc for Matchers
class
clazz
- Type to avoid castingnull
.public static String contains(String substring)
String
argument that contains the given substring.
See examples in javadoc for Matchers
class
substring
- the substring.public static String matches(String regex)
String
argument that matches the given regular expression.
See examples in javadoc for Matchers
class
regex
- the regular expression.public static String endsWith(String suffix)
String
argument that ends with the given suffix.
See examples in javadoc for Matchers
class
suffix
- the suffix.public static String startsWith(String prefix)
String
argument that starts with the given prefix.
See examples in javadoc for Matchers
class
prefix
- the prefix.public static <T> T argThat(ArgumentMatcher<T> matcher)
ArgumentMatcher
for rationale and migration guide.
NullPointerException auto-unboxing caveat is described below.
It is important to understand the use cases and available options for dealing with non-trivial arguments
before implementing custom argument matchers. This way, you can select the best possible approach
for given scenario and produce highest quality test (clean and maintainable).
Please read the documentation for ArgumentMatcher
to learn about approaches and see the examples.
NullPointerException auto-unboxing caveat.
In rare cases when matching primitive parameter types you *must* use relevant intThat(), floatThat(), etc. method.
This way you will avoid NullPointerException
during auto-unboxing.
Due to how java works we don't really have a clean way of detecting this scenario and protecting the user from this problem.
Hopefully, the javadoc describes the problem and solution well.
If you have an idea how to fix the problem, let us know via the mailing list or the issue tracker.
See examples in javadoc for ArgumentMatcher
class
matcher
- decides whether argument matchesnull
.public static char charThat(ArgumentMatcher<Character> matcher)
char
argument matchers.
Note that argThat(org.mockito.ArgumentMatcher<T>)
will not work with primitive char
matchers due to NullPointerException
auto-unboxing caveat.
See examples in javadoc for Matchers
class
matcher
- decides whether argument matches0
.public static boolean booleanThat(ArgumentMatcher<Boolean> matcher)
boolean
argument matchers.
Note that argThat(org.mockito.ArgumentMatcher<T>)
will not work with primitive boolean
matchers due to NullPointerException
auto-unboxing caveat.
See examples in javadoc for Matchers
class
matcher
- decides whether argument matchesfalse
.public static byte byteThat(ArgumentMatcher<Byte> matcher)
byte
argument matchers.
Note that argThat(org.mockito.ArgumentMatcher<T>)
will not work with primitive byte
matchers due to NullPointerException
auto-unboxing caveat.
See examples in javadoc for Matchers
class
matcher
- decides whether argument matches0
.public static short shortThat(ArgumentMatcher<Short> matcher)
short
argument matchers.
Note that argThat(org.mockito.ArgumentMatcher<T>)
will not work with primitive short
matchers due to NullPointerException
auto-unboxing caveat.
See examples in javadoc for Matchers
class
matcher
- decides whether argument matches0
.public static int intThat(ArgumentMatcher<Integer> matcher)
int
argument matchers.
Note that argThat(org.mockito.ArgumentMatcher<T>)
will not work with primitive int
matchers due to NullPointerException
auto-unboxing caveat.
See examples in javadoc for Matchers
class
matcher
- decides whether argument matches0
.public static long longThat(ArgumentMatcher<Long> matcher)
long
argument matchers.
Note that argThat(org.mockito.ArgumentMatcher<T>)
will not work with primitive long
matchers due to NullPointerException
auto-unboxing caveat.
See examples in javadoc for Matchers
class
matcher
- decides whether argument matches0
.public static float floatThat(ArgumentMatcher<Float> matcher)
float
argument matchers.
Note that argThat(org.mockito.ArgumentMatcher<T>)
will not work with primitive float
matchers due to NullPointerException
auto-unboxing caveat.
See examples in javadoc for Matchers
class
matcher
- decides whether argument matches0
.public static double doubleThat(ArgumentMatcher<Double> matcher)
double
argument matchers.
Note that argThat(org.mockito.ArgumentMatcher<T>)
will not work with primitive double
matchers due to NullPointerException
auto-unboxing caveat.
See examples in javadoc for Matchers
class
matcher
- decides whether argument matches0
.