org.mockito.hamcrest
public class MockitoHamcrest extends Object
ArgumentMatcher
.
Mockito 2.1.0 was decoupled from Hamcrest to avoid version incompatibilities
that have impacted our users in past. Mockito offers a dedicated API to match arguments
via ArgumentMatcher
.
Hamcrest integration is provided so that users can take advantage of existing Hamcrest matchers.
Example:
import static org.mockito.hamcrest.MockitoHamcrest.argThat; //stubbing when(mock.giveMe(argThat(new MyHamcrestMatcher()))); //verification verify(mock).giveMe(argThat(new MyHamcrestMatcher()));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.Constructor and Description |
---|
MockitoHamcrest() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
argThat(org.hamcrest.Matcher<T> matcher)
Allows matching arguments with hamcrest matchers.
|
static boolean |
booleanThat(org.hamcrest.Matcher<Boolean> matcher)
Enables integrating hamcrest matchers that match primitive
boolean arguments. |
static byte |
byteThat(org.hamcrest.Matcher<Byte> matcher)
Enables integrating hamcrest matchers that match primitive
byte arguments. |
static char |
charThat(org.hamcrest.Matcher<Character> matcher)
Enables integrating hamcrest matchers that match primitive
char arguments. |
static double |
doubleThat(org.hamcrest.Matcher<Double> matcher)
Enables integrating hamcrest matchers that match primitive
double arguments. |
static float |
floatThat(org.hamcrest.Matcher<Float> matcher)
Enables integrating hamcrest matchers that match primitive
float arguments. |
static int |
intThat(org.hamcrest.Matcher<Integer> matcher)
Enables integrating hamcrest matchers that match primitive
int arguments. |
static long |
longThat(org.hamcrest.Matcher<Long> matcher)
Enables integrating hamcrest matchers that match primitive
long arguments. |
static short |
shortThat(org.hamcrest.Matcher<Short> matcher)
Enables integrating hamcrest matchers that match primitive
short arguments. |
public static <T> T argThat(org.hamcrest.Matcher<T> matcher)
MockitoHamcrest
classmatcher
- decides whether argument matchesnull
or default value for primitive (0, false, etc.)public static char charThat(org.hamcrest.Matcher<Character> matcher)
char
arguments.
Note that argThat(org.hamcrest.Matcher)
will not work with primitive char
matchers due to NullPointerException
auto-unboxing caveat.
See examples in javadoc for MockitoHamcrest
classmatcher
- decides whether argument matches0
.public static boolean booleanThat(org.hamcrest.Matcher<Boolean> matcher)
boolean
arguments.
Note that argThat(org.hamcrest.Matcher)
will not work with primitive boolean
matchers due to NullPointerException
auto-unboxing caveat.
See examples in javadoc for MockitoHamcrest
classmatcher
- decides whether argument matchesfalse
.public static byte byteThat(org.hamcrest.Matcher<Byte> matcher)
byte
arguments.
Note that argThat(org.hamcrest.Matcher)
will not work with primitive byte
matchers due to NullPointerException
auto-unboxing caveat.
* See examples in javadoc for MockitoHamcrest
classmatcher
- decides whether argument matches0
.public static short shortThat(org.hamcrest.Matcher<Short> matcher)
short
arguments.
Note that argThat(org.hamcrest.Matcher)
will not work with primitive short
matchers due to NullPointerException
auto-unboxing caveat.
* See examples in javadoc for MockitoHamcrest
classmatcher
- decides whether argument matches0
.public static int intThat(org.hamcrest.Matcher<Integer> matcher)
int
arguments.
Note that argThat(org.hamcrest.Matcher)
will not work with primitive int
matchers due to NullPointerException
auto-unboxing caveat.
* See examples in javadoc for MockitoHamcrest
classmatcher
- decides whether argument matches0
.public static long longThat(org.hamcrest.Matcher<Long> matcher)
long
arguments.
Note that argThat(org.hamcrest.Matcher)
will not work with primitive long
matchers due to NullPointerException
auto-unboxing caveat.
* See examples in javadoc for MockitoHamcrest
classmatcher
- decides whether argument matches0
.public static float floatThat(org.hamcrest.Matcher<Float> matcher)
float
arguments.
Note that argThat(org.hamcrest.Matcher)
will not work with primitive float
matchers due to NullPointerException
auto-unboxing caveat.
* See examples in javadoc for MockitoHamcrest
classmatcher
- decides whether argument matches0
.public static double doubleThat(org.hamcrest.Matcher<Double> matcher)
double
arguments.
Note that argThat(org.hamcrest.Matcher)
will not work with primitive double
matchers due to NullPointerException
auto-unboxing caveat.
* See examples in javadoc for MockitoHamcrest
classmatcher
- decides whether argument matches0
.