Course – LS – All

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


1. Overview

In certain scenarios, we need to assert if a given String is empty or not. There are quite a few ways to do such assertions in Java.

Let’s explore some of those test assertion techniques in this quick tutorial.

2. Maven Dependencies

We need to grab a few dependencies first. In a Maven project, we can add following dependencies to the pom.xml:



Hamcrest Core:


Apache Commons Lang:




Google Guava:


3. Using JUnit

We’ll use the isEmpty method from the String class along with the Assert class from JUnit to verify whether a given String isn’t empty. Since the isEmpty method returns true if the input String is empty we can use it together with the assertFalse method:


Or, we can also use:


Thought since text might be null, another way is doing an equality check by using the assertNotEquals method:

assertNotEquals("", text);


assertNotSame("", text);

Check out our in-depth guide on JUnit assertions here.

All these assertions, when failed, will return an AssertionError.

4. Using Hamcrest Core

Hamcrest is a well-known framework providing matchers that are commonly used for unit testing in the Java ecosystem.

We can make use of the Hamcrest CoreMatchers class for empty String checking:

assertThat(text, CoreMatchers.not(isEmptyString()));

The isEmptyString method is available in the IsEmptyString class.

This also returns an AssertionError when failing, but with a more helpful output:

Expected: not an empty string
     but: was ""

If required, to verify that a String is neither empty nor null, we can use the isEmptyOrNullString:

assertThat(text, CoreMatchers.not(isEmptyOrNullString()));

To learn about other methods of the CoreMatchers class read this previously published article.

5. Using Apache Commons Lang

The Apache Commons Lang library provides a host of helper utilities for the java.lang API.

The StringUtils class offers a method that we can use to check for empty Strings:


When failing, this returns a simple AssertionError.

To learn more about String processing with Apache Commons Lang read this article.

6. Using AssertJ

AssertJ is an open source, a community-driven library used for writing fluent and rich assertions in Java tests.

The method AbstractCharSequenceAssert.isNotEmpty() verifies that the actual CharSequence is not empty, or, in other words, that it is not null and has a length of 1 or more:


When failing, this prints the output:

Expecting actual not to be empty

We have a nice introductory article on AssertJ here.

7. Using Google Guava

Guava is a set of core libraries offered by Google.

The method isNullOrEmpty from the Guava Strings class can be utilized to verify if a String is empty (or null):


This also returns an AssertionError when failing with no other output message.

To explore our other articles on the Guava API follow the link here.

8. Conclusion

In this quick tutorial, we found out how to assert if a given String is empty or not.

As always the full code snippet is available in over on GitHub.

Course – LS – All

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

res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.