Hamcrest is library of matcher objects to define matching rules declaratively. You get a Java DSL for buildung assertions, constraints and predicates in quite more readable fashion. Typical scenarios are writing of assertions in (unit) tests, expectations in mocking frameworks, defining of UI validation rules and processing of collections. Different Java frameworks use Hamcrest, including JUnit (since 4.4) and JMock (origin of Hamcrest). See project homepage for more information.
- Assertions in JUnit
assertThat(x, is(not(4))); assertThat(color, either(containsString("red")).or(containsString("blue")));
- Filtering of collections
List<person> germans = select(people, where(Person.class).getNationality(), equalToIgnoringCase("german"));