Matching rules with Hamcrest

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.

Examples:

  • 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"));
Short URL for this post: http://wp.me/p4nxik-70
This entry was posted in Java and Quality and tagged , , , , , . Bookmark the permalink.