Package org.mockito.hamcrest
Class MockitoHamcrest
java.lang.Object
org.mockito.hamcrest.MockitoHamcrest
Allows matching arguments with hamcrest matchers.
Requires hamcrest on classpath,
Mockito does not depend on hamcrest!
Note the NullPointerException auto-unboxing caveat described below.
Before implementing or reusing an existing hamcrest matcher please read
how to deal with sophisticated argument matching in
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.- Since:
- 2.1.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TargThat(org.hamcrest.Matcher<T> matcher) Allows matching arguments with hamcrest matchers.static booleanbooleanThat(org.hamcrest.Matcher<Boolean> matcher) Enables integrating hamcrest matchers that match primitivebooleanarguments.static byteEnables integrating hamcrest matchers that match primitivebytearguments.static charEnables integrating hamcrest matchers that match primitivechararguments.static doubledoubleThat(org.hamcrest.Matcher<Double> matcher) Enables integrating hamcrest matchers that match primitivedoublearguments.static floatEnables integrating hamcrest matchers that match primitivefloatarguments.static intEnables integrating hamcrest matchers that match primitiveintarguments.static longEnables integrating hamcrest matchers that match primitivelongarguments.private static <T> voidreportMatcher(org.hamcrest.Matcher<T> matcher) static shortEnables integrating hamcrest matchers that match primitiveshortarguments.
-
Constructor Details
-
MockitoHamcrest
private MockitoHamcrest()
-
-
Method Details
-
argThat
public static <T> T argThat(org.hamcrest.Matcher<T> matcher) Allows matching arguments with hamcrest matchers. See examples in javadoc forMockitoHamcrestclass- Parameters:
matcher- decides whether argument matches- Returns:
nullor default value for primitive (0, false, etc.)- Since:
- 2.1.0
-
charThat
Enables integrating hamcrest matchers that match primitivechararguments. Note thatargThat(org.hamcrest.Matcher<T>)will not work with primitivecharmatchers due toNullPointerExceptionauto-unboxing caveat. See examples in javadoc forMockitoHamcrestclass- Parameters:
matcher- decides whether argument matches- Returns:
0.
-
booleanThat
Enables integrating hamcrest matchers that match primitivebooleanarguments. Note thatargThat(org.hamcrest.Matcher<T>)will not work with primitivebooleanmatchers due toNullPointerExceptionauto-unboxing caveat. See examples in javadoc forMockitoHamcrestclass- Parameters:
matcher- decides whether argument matches- Returns:
false.
-
byteThat
Enables integrating hamcrest matchers that match primitivebytearguments. Note thatargThat(org.hamcrest.Matcher<T>)will not work with primitivebytematchers due toNullPointerExceptionauto-unboxing caveat. * See examples in javadoc forMockitoHamcrestclass- Parameters:
matcher- decides whether argument matches- Returns:
0.
-
shortThat
Enables integrating hamcrest matchers that match primitiveshortarguments. Note thatargThat(org.hamcrest.Matcher<T>)will not work with primitiveshortmatchers due toNullPointerExceptionauto-unboxing caveat. * See examples in javadoc forMockitoHamcrestclass- Parameters:
matcher- decides whether argument matches- Returns:
0.
-
intThat
Enables integrating hamcrest matchers that match primitiveintarguments. Note thatargThat(org.hamcrest.Matcher<T>)will not work with primitiveintmatchers due toNullPointerExceptionauto-unboxing caveat. * See examples in javadoc forMockitoHamcrestclass- Parameters:
matcher- decides whether argument matches- Returns:
0.
-
longThat
Enables integrating hamcrest matchers that match primitivelongarguments. Note thatargThat(org.hamcrest.Matcher<T>)will not work with primitivelongmatchers due toNullPointerExceptionauto-unboxing caveat. * See examples in javadoc forMockitoHamcrestclass- Parameters:
matcher- decides whether argument matches- Returns:
0.
-
floatThat
Enables integrating hamcrest matchers that match primitivefloatarguments. Note thatargThat(org.hamcrest.Matcher<T>)will not work with primitivefloatmatchers due toNullPointerExceptionauto-unboxing caveat. * See examples in javadoc forMockitoHamcrestclass- Parameters:
matcher- decides whether argument matches- Returns:
0.
-
doubleThat
Enables integrating hamcrest matchers that match primitivedoublearguments. Note thatargThat(org.hamcrest.Matcher<T>)will not work with primitivedoublematchers due toNullPointerExceptionauto-unboxing caveat. * See examples in javadoc forMockitoHamcrestclass- Parameters:
matcher- decides whether argument matches- Returns:
0.
-
reportMatcher
private static <T> void reportMatcher(org.hamcrest.Matcher<T> matcher)
-