Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.12.0</version>
</dependency>

AssertJ:

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

Google Guava:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW