I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE 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:

JUnit:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

Hamcrest Core:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-core</artifactId>
    <version>1.3</version>
</dependency>

Apache Commons Lang:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8</version>
</dependency>

AssertJ:

<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.11.1</version>
</dependency>

Google Guava:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>26.0-jre</version>
</dependency>

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:

assertFalse(text.isEmpty());

Or, we can also use:

assertTrue(!text.isEmpty());

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

assertNotEquals("", text);

Or:

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:

java.lang.AssertionError: 
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:

assertTrue(StringUtils.isNotBlank(text));

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:

Assertions.assertThat(text).isNotEmpty()

When failing, this prints the output:

java.lang.AssertionError: 
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):

assertFalse(Strings.isNullOrEmpty(text));

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.

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE LESSONS

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Michèl Breevoort Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Michèl Breevoort
Guest

Nice article. Maybe it is a good idea to mention or add examples what will be the error when the assert fails. That’s why I opt for the Hamcrest Core with assertThat because the failure will show the expected and the actual result very clear. All the asserts with boolean are not recommended because they will only show true/false on failure.