Hamcrest Library

In this lesson, we'll learn how to write assertions using the Hamcrest library.

What is the Hamcrest library?

Hamcrest is a framework that comes with a library of useful matchers for writing match rules using matcher objects.

The matchers are most commonly used for:

  1. Writing test assertions
  2. Data validation
  3. Filtering

Note: In Java, we use the Matcher class to search for the regular expression in a particular piece of text.

Examples

We will use Hamcrest with TestNG to demonstrate its usage.

Let’s take a look at the code below and its explanation in the method comments.

Get hands-on with 1400+ tech skills courses.