Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


1. Overview

In this tutorial, we'll explore Hamcrest Text Matchers.

We discussed Hamcrest Matchers in general before in testing with Hamcrest, in this tutorial we'll focus on Text Matchers only.

2. Maven Configuration

First, we need to add the following dependency to our pom.xml:


The latest version of java-hamcrest can be downloaded from Maven Central.

Now, we'll dive right into Hamcrest Text Matchers.

3. Text Equality Matchers

We can, of course, check if two Strings are equal using the standard isEqual() matcher.

In addition, we have two matchers that are specific to String types: equalToIgnoringCase() and equalToIgnoringWhiteSpace().

Let's check if two Strings are equal – ignoring case:

public void whenTwoStringsAreEqual_thenCorrect() {
    String first = "hello";
    String second = "Hello";

    assertThat(first, equalToIgnoringCase(second));

We can also check if two Strings are equal – ignoring leading and trailing whitespace:

public void whenTwoStringsAreEqualWithWhiteSpace_thenCorrect() {
    String first = "hello";
    String second = "   Hello   ";

    assertThat(first, equalToIgnoringWhiteSpace(second));

4. Empty Text Matchers

We can check if a String is blank, meaning it contains only whitespace, by using the blankString() and blankOrNullString() matchers:

public void whenStringIsBlank_thenCorrect() {
    String first = "  ";
    String second = null;
    assertThat(first, blankString());
    assertThat(first, blankOrNullString());
    assertThat(second, blankOrNullString());

On the other hand, if we want to verify if a String is empty, we can use the emptyString() matchers:

public void whenStringIsEmpty_thenCorrect() {
    String first = "";
    String second = null;

    assertThat(first, emptyString());
    assertThat(first, emptyOrNullString());
    assertThat(second, emptyOrNullString());

5. Pattern Matchers

We can also check if a given text matches a regular expression using the matchesPattern() function:

public void whenStringMatchPattern_thenCorrect() {
    String first = "hello";

    assertThat(first, matchesPattern("[a-z]+"));

6. Sub-String Matchers

We can determine if a text contains another sub-text by using the containsString() function or containsStringIgnoringCase():

public void whenVerifyStringContains_thenCorrect() {
    String first = "hello";

    assertThat(first, containsString("lo"));
    assertThat(first, containsStringIgnoringCase("EL"));

If we expect the sub-strings to be in a specific order, we can call the stringContainsInOrder() matcher:

public void whenVerifyStringContainsInOrder_thenCorrect() {
    String first = "hello";
    assertThat(first, stringContainsInOrder("e","l","o"));

Next, let's see how to check that a String starts with a given String:

public void whenVerifyStringStartsWith_thenCorrect() {
    String first = "hello";

    assertThat(first, startsWith("he"));
    assertThat(first, startsWithIgnoringCase("HEL"));

And finally, we can check if a String ends with a specified String:

public void whenVerifyStringEndsWith_thenCorrect() {
    String first = "hello";

    assertThat(first, endsWith("lo"));
    assertThat(first, endsWithIgnoringCase("LO"));

7. Conclusion

In this quick tutorial, we explored Hamcrest Text Matchers.

As always, the full source code for the examples can be found over on GitHub.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Junit footer banner
Comments are closed on this article!