Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

String Not Empty Test Assertions in Java

$
0
0

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.

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.

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));

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()

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));

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.


Viewing all articles
Browse latest Browse all 4535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>