se.mockachino.matchers
Class Matchers

java.lang.Object
  extended by se.mockachino.matchers.MatchersBase
      extended by se.mockachino.matchers.Matchers

public class Matchers
extends MatchersBase

Matchers is a utility class, containing a lot of useful default matchers.

A matcher is just a plain java object of the Matcher type, which can be used by verifying and stubbing.

The only important thing is that when using a matcher for a method call, you need to wrap it in Matchers.match() (or Matchers.m() as a shortcut).

This wrapping MUST be directly in the actual method call for it to work. Example:

 List myMock = mock(List.class);
 Matcher myMatcher = new Matcher{...};
 

// won't work, get expects an integer, not a matcher stubReturn(1).on(myMock).get(myMatcher));

// Will work: stubReturn(1).on(myMock).get(match(myMatcher)));

Apart from using custom matchers, you can use some predefined ones. All methods here that return a Matcher needs to be wrapped in Matchers.match() when using in an argument. The methods that return a primitive or a <T> have already been wrapped in Matchers.match() and can be used directly as is.

Example:

 stubReturn(1).on(myMock).get(match(mAnyInt()));
 stubReturn(1).on(myMock).get(m(mAnyInt()));
 stubReturn(1).on(myMock).get(anyInt());
 


Constructor Summary
Matchers()
           
 
Method Summary
static
<T> T
and(Matcher<T>... matchers)
          Matches the parameter if and only if all the matchers match.
static
<T> T
any(java.lang.Class<T> clazz)
          Always matches.
static boolean anyBoolean()
          Always matches.
static byte anyByte()
          Always matches.
static char anyChar()
          Always matches.
static double anyDouble()
          Always matches.
static float anyFloat()
          Always matches.
static int anyInt()
          Always matches.
static long anyLong()
          Always matches.
static short anyShort()
          Always matches.
static java.lang.String contains(java.lang.String s)
          Matches if the parameter value contains s.
static java.lang.String endsWith(java.lang.String s)
          Matches if the parameter value ends with s.
static
<T> T
eq(T value)
          Compares value with the actual parameter based on equality.
static
<T> T
m(Matcher<T> matcher)
          Shortcut for match(se.mockachino.matchers.matcher.Matcher)
static
<T> T
match(Matcher<T> matcher)
          Matches using a custom matcher.
static
<T> T
not(Matcher<T> matcher)
          Negates the matching of the specified matcher.
static
<T> T
notEq(T value)
          Matches negation of eq(T)
static
<T> T
notSame(T value)
          Matches negation of same(T)
static
<T> T
or(Matcher<T>... matchers)
          Matches the parameter if any of the matchers match.
static java.lang.String regexp(java.lang.String s)
          Matches if the parameter value matches the regular expression.
static
<T> T
same(T value)
          Compares value with the actual parameter based on object identity.
static java.lang.String startsWith(java.lang.String s)
          Matches if the parameter value starts with s.
static
<T> T
type(java.lang.Class<T> clazz, java.lang.Class<?>... classes)
          Matches if the argument is an instance of any of the classes.
 
Methods inherited from class se.mockachino.matchers.MatchersBase
mAnd, mAny, mAnyBoolean, mAnyByte, mAnyChar, mAnyDouble, mAnyFloat, mAnyInt, mAnyLong, mAnyShort, matchEq, mContains, mEndsWith, mEq, mNot, mOr, mRegexp, mSame, mStartsWith, mType
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Matchers

public Matchers()
Method Detail

match

public static <T> T match(Matcher<T> matcher)
Matches using a custom matcher.

Parameters:
matcher -

m

public static <T> T m(Matcher<T> matcher)
Shortcut for match(se.mockachino.matchers.matcher.Matcher)


same

public static <T> T same(T value)
Compares value with the actual parameter based on object identity. Don't use this matcher for primitive data types.

Parameters:
value - the value
Returns:

notSame

public static <T> T notSame(T value)
Matches negation of same(T)


eq

public static <T> T eq(T value)
Compares value with the actual parameter based on equality. For primitive arrays, it compares based in array content, instead of array identity. This is also recursive, so it handles values such as int[][], et.c.

Parameters:
value -

notEq

public static <T> T notEq(T value)
Matches negation of eq(T)


and

public static <T> T and(Matcher<T>... matchers)
Matches the parameter if and only if all the matchers match.

If the list of matchers is empty, it defaults to true.

Parameters:
matchers -

or

public static <T> T or(Matcher<T>... matchers)
Matches the parameter if any of the matchers match.

If the list of matchers is empty, it defaults to false.

Parameters:
matchers -

not

public static <T> T not(Matcher<T> matcher)
Negates the matching of the specified matcher.

Parameters:
matcher -

contains

public static java.lang.String contains(java.lang.String s)
Matches if the parameter value contains s.

Parameters:
s - the string to search for

startsWith

public static java.lang.String startsWith(java.lang.String s)
Matches if the parameter value starts with s.

Parameters:
s - the string to search for

endsWith

public static java.lang.String endsWith(java.lang.String s)
Matches if the parameter value ends with s.

Parameters:
s - the string to search for

regexp

public static java.lang.String regexp(java.lang.String s)
Matches if the parameter value matches the regular expression.

Parameters:
s - the string to search for

anyBoolean

public static boolean anyBoolean()
Always matches.


any

public static <T> T any(java.lang.Class<T> clazz)
Always matches.


anyChar

public static char anyChar()
Always matches.


anyLong

public static long anyLong()
Always matches.


anyFloat

public static float anyFloat()
Always matches.


anyDouble

public static double anyDouble()
Always matches.


anyShort

public static short anyShort()
Always matches.


anyByte

public static byte anyByte()
Always matches.


anyInt

public static int anyInt()
Always matches.


type

public static <T> T type(java.lang.Class<T> clazz,
                         java.lang.Class<?>... classes)
Matches if the argument is an instance of any of the classes.