Close

Mockito - Argument Matchers

[Last Updated: Aug 11, 2020]

The static methods of Mockito's ArgumentMatchers class allows us to pass flexible arguments while stubbing and verification.

Following quick example uses ArgumentMatchers.anyInt() which represents any int value:

Mockito.when(myService.doSomething(ArgumentMatchers.anyInt())).thenReturn(5);

ArgumentMatchers methods

Any Object

public static <T> T any()//Matches anything, including nulls and varargs.
public static <T> T any(Class<T> type)//Matches any object of given type
public static <T> T isA(Class<T> type)//Object argument that implements the given class.

Primitive matchers

public static boolean anyBoolean()
public static byte anyByte()
public static char anyChar()
public static int anyInt()
public static long anyLong()
public static float anyFloat()
public static double anyDouble()
public static short anyShort()

String matcher

public static String anyString()
public static String contains(String substring)
public static String matches(String regex)
public static String matches(Pattern pattern)
public static String endsWith(String suffix)
public static String startsWith(String prefix)

Collection matchers

public static <T> List<T> anyList()
public static <T> Set<T> anySet()
public static <K,V> Map<K,V> anyMap()
public static <T> Collection<T> anyCollection()
public static <T> Iterable<T> anyIterable()

Equal to given value

public static boolean eq(boolean value)
public static byte eq(byte value)
public static char eq(char value)
public static double eq(double value)
public static float eq(float value)
public static int eq(int value)
public static long eq(long value)
public static short eq(short value)
public static <T> T eq(T value)

public static <T> T refEq(T value, String... excludeFields)
//Above method can be used when equals() is not implemented on compared objects. 
//It uses java reflection API to compare fields of wanted and actual object.

public static <T> T same(T value)
//Above method uses  == operator for equality check

Null Arguments

public static <T> T isNull()
public static <T> T notNull()
public static <T> T isNotNull()//Alias to noNull()
public static <T> T nullable(Class<T> clazz)//Argument that is either null or of the given type.

Custom Argument Matchers

public static <T> T argThat(ArgumentMatcher<T> matcher)
public static char charThat(ArgumentMatcher<Character> matcher)
public static boolean booleanThat(ArgumentMatcher<Boolean> matcher)
public static byte byteThat(ArgumentMatcher<Byte> matcher)
public static short shortThat(ArgumentMatcher<Short> matcher)
public static int intThat(ArgumentMatcher<Integer> matcher)
public static long longThat(ArgumentMatcher<Long> matcher)
public static float floatThat(ArgumentMatcher<Float> matcher)
public static double doubleThat(ArgumentMatcher<Double> matcher)

Following is ArgumentMatcher interface for custom matcher implementation:

package org.mockito;

public interface ArgumentMatcher<T> {
    boolean matches(T argument);
}

See Also