Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the main differences between Mockito matchers and Hamcrest matchers


What are the main differences between Mockito matchers and Hamcrest matchers


Mockito matchers and Hamcrest matchers are both used in Java for creating test assertions, but they serve different purposes and have distinct characteristics.

Mockito Matchers

Mockito matchers are primarily used for setting up expectations or verifications when working with mock objects. These matchers return the type of the object being matched, which allows them to be used directly as arguments in method calls on mock objects. For example, `org.mockito.Matchers.any(Integer.class)` returns an `Integer`, making it suitable for use in method stubbing or verification, such as `when(mockFoo.getBarByIndex(any(Integer.class)))`[1].

Mockito matchers are designed to work seamlessly with Mockito's mocking framework, allowing you to define flexible expectations for method calls on mock objects. However, they are not typically used with `assertThat` statements, which are more commonly associated with Hamcrest.

Hamcrest Matchers

Hamcrest matchers, on the other hand, are designed to be used with `assertThat` statements for asserting conditions in tests. They return a `Matcher` object, which is a predicate that can be used to evaluate whether a condition is met. Hamcrest matchers are highly flexible and can be combined using logical operators like `allOf`, `anyOf`, and `not` to create complex assertions[2][3].

Hamcrest matchers are widely used in conjunction with JUnit and other testing frameworks to write clear and readable test assertions. They provide a fluent API that makes test code more self-documenting and easier to understand. For example, `assertThat("Hello", equalTo("Hello"))` clearly communicates the intent of the assertion[7].

Integration Between Mockito and Hamcrest

In cases where you want to use a Hamcrest matcher in a context that requires a Mockito matcher (e.g., stubbing or verifying a method call), Mockito provides the `argThat` method. This method allows you to wrap a Hamcrest matcher and use it as a Mockito matcher. For example, `when(mockFoo.getBarByDouble(argThat(closeTo(1.0, 0.001))))` uses a Hamcrest matcher to specify a condition for a double value with some precision[1][5].

In Mockito 2 and later, the integration with Hamcrest is handled through the `MockitoHamcrest` class, which provides an `argThat` method specifically designed to work with Hamcrest matchers[5][10]. This decoupling helps avoid version conflicts between Mockito and Hamcrest libraries.

In summary, Mockito matchers are primarily used for mocking and stubbing, while Hamcrest matchers are used for assertions. Both can be used together effectively in tests by leveraging the `argThat` method to integrate Hamcrest matchers into Mockito's mocking framework.

Citations:
[1] https://stackoverflow.com/questions/8348046/mockitos-matcher-vs-hamcrest-matcher
[2] https://www.javacodegeeks.com/2015/11/hamcrest-matchers-tutorial.html
[3] https://www.vogella.com/tutorials/Hamcrest/article.html
[4] https://support.intershop.com/kb/go.php/a/ENFDEVDOC/pages/1827636063/Guide+-+7.10+Migration+Mockito+1+to+Mockito+2
[5] https://site.mockito.org/javadoc/current/org/mockito/hamcrest/MockitoHamcrest.html
[6] https://www.baeldung.com/hamcrest-text-matchers
[7] https://automatenow.io/complete-guide-to-hamcrest-matchers/
[8] https://www.scalatest.org/scaladoc/3.0.7/org/scalatest/Matchers.html
[9] https://hamcrest.org/JavaHamcrest/tutorial
[10] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.hamcrest.MockitoHamcrest.argThat