-
any()
checks absolutely nothing. Since Mockito 2.0,any(T.class)
sharesisA
semantics to mean “anyT
” or properly “any instance of typeT
“.This is a change compared to Mockito 1.x, where
any(T.class)
checked absolutely nothing but saved a cast prior to Java 8: “Any kind object, not necessary of the given class. The class argument is provided only to avoid casting.” -
isA(T.class)
checks that the argumentinstanceof T
, implying it is non-null. -
same(obj)
checks that the argument refers to the same instance asobj
, such thatarg == obj
is true. -
eq(obj)
checks that the argument equalsobj
according to itsequals
method. This is also the behavior if you pass in real values without using matchers.Note that unless
equals
is overridden, you’ll see the default Object.equals implementation, which would have the same behavior assame(obj)
.
If you need more exact customization, you can use an adapter for your own predicate:
- For Mockito 1.x, use
argThat
with a custom HamcrestMatcher<T>
that selects exactly the objects you need. - For Mockito 2.0 and beyond, use
Matchers.argThat
with a customorg.mockito.ArgumentMatcher<T>
, orMockitoHamcrest.argThat
with a custom HamcrestMatcher<T>
.
You may also use refEq
, which uses reflection to confirm object equality; Hamcrest has a similar implementation with SamePropertyValuesAs for public bean-style properties. Note that on GitHub issue #1800 proposes deprecating and removing refEq
, and as in that issue you might prefer eq
to better give your classes better encapsulation over their sense of equality.