This is going to be an easy one, but I cannot find the difference between them and which one to use, if I have both the lib’s included in my classpath?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Hamcrest matcher methods return
Matcher<T>and Mockito matchers return T. So, for example:org.hamcrest.Matchers.any(Integer.class)returns an instance oforg.hamcrest.Matcher<Integer>, andorg.mockito.Matchers.any(Integer.class)returns an instance ofInteger.That means that you can only use Hamcrest matchers when a
Matcher<?>object is expected in the signature – typically, inassertThatcalls. When setting up expectations or verifications where you are calling methods of the mock object, you use the Mockito matchers.For example (with fully qualified names for clarity):
If you want to use a Hamcrest matcher in a context that requires a Mockito matcher, you can use the
org.mockito.Matchers.argThat(ororg.mockito.hamcrest.MockitoHamcrest.argThatin Mockito 2). It converts a Hamcrest matcher into a Mockito matcher. So, say you wanted to match a double value with some precision (but not much). In that case, you could do: