Hamcrest

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:

testCompile 'org.hamcrest:hamcrest-all:1.3' 

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)); ...

Get Test-Driven Java Development - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.