Hamcrest adds a lot of methods called matchers. Each matcher is designed to perform a comparison operation. It is extensible enough to support custom matchers created by yourself. Furthermore, JUnit supports Hamcrest natively since its core is included in the JUnit distribution. You can start using Hamcrest effortlessly. However, we want to use the full-featured project so we will add a test dependency to Gradle's file:
Let us compare one assert from JUnit with the equivalent one from Hamcrest:
- The JUnit assert:
List<String> friendsOfJoe = Arrays.asList("Audrey", "Peter", "Michael", "Britney", "Paul");Assert.assertTrue( friendships.getFriendsList("Joe") .containsAll(friendsOfJoe)); ...