Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Are there any alternatives to Hamcrest that work better with Mockito


Are there any alternatives to Hamcrest that work better with Mockito


When working with Mockito, Hamcrest is often used for its expressive matchers, which enhance the readability of tests. However, there are alternatives that can work well with Mockito, each with their own strengths and weaknesses.

AssertJ

AssertJ is a popular alternative to Hamcrest for writing assertions in unit tests. It provides a fluent API that many developers find easier to use and more expressive than Hamcrest. AssertJ's syntax is often preferred for its readability and the ability to easily create custom assertions. While AssertJ does not directly integrate with Mockito like Hamcrest does, it can be used alongside Mockito for assertions, making tests more readable and maintainable. However, AssertJ is primarily focused on assertions rather than the broader matching capabilities of Hamcrest[1][3].

Truth

Truth is another assertion library developed by Google. It offers a fluent API similar to AssertJ but focuses more on providing better auto-completion support in IDEs, which can be beneficial for discovering available assertions. Truth is not as commonly used with Mockito as Hamcrest but can be used for assertions in tests. Like AssertJ, Truth is more focused on assertions rather than the general-purpose matching that Hamcrest provides[3].

Mockito's Built-in Matchers

Mockito itself provides matchers that can be used for stubbing and verifying interactions with mock objects. These matchers return the type they are matching (e.g., `any(Integer.class)` returns an `Integer`), which is different from Hamcrest matchers that return a `Matcher`. To use Hamcrest matchers with Mockito, you can use `argThat` or `MockitoHamcrest.argThat` to convert a Hamcrest matcher into a Mockito-compatible form[7].

Custom Implementations

For scenarios where you prefer not to use external libraries, you can implement your own matchers or assertions. This approach allows for complete control over the behavior and syntax but requires more effort and maintenance. It's typically not recommended unless you have very specific needs that aren't met by existing libraries.

Conclusion

While Hamcrest is widely used with Mockito due to its flexibility and expressiveness, alternatives like AssertJ and Truth can enhance the assertion part of your tests. If you're looking for something that integrates well with Mockito for matching purposes, Hamcrest remains a strong choice, but you can use its matchers alongside other assertion libraries for a more comprehensive testing experience. Ultimately, the choice depends on your specific needs and preferences regarding readability, ease of use, and the type of assertions or matches you need to perform in your tests.

Citations:
[1] https://www.reddit.com/r/java/comments/nj7dvd/who_here_are_using_the_hamcrest_api_and_why/
[2] https://www.javacodegeeks.com/2015/11/hamcrest-matchers-tutorial.html
[3] https://truth.dev/comparison.html
[4] https://www.reddit.com/r/javahelp/comments/oqz8cn/unit_testing_with_mockito_framework_why_do_we/
[5] https://www.infoworld.com/article/2257253/junit-5-tutorial-part-1-unit-testing-with-junit-5-mockito-and-hamcrest.html
[6] https://softwareengineering.stackexchange.com/questions/447953/alternatives-to-using-mock-libraries-such-as-mockito-in-unit-testing
[7] https://stackoverflow.com/questions/8348046/mockitos-matcher-vs-hamcrest-matcher
[8] https://stackoverflow.com/questions/12606196/what-is-the-alternative-to-using-the-deprecated-hamcrest-method-is