1. Overview
1.概述
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.
在某些情况下,我们需要断言一个给定的String是否为空。在Java中,有很多方法可以做这样的断言。
Let’s explore some of those test assertion techniques in this quick tutorial.
让我们在这个快速教程中探索其中一些测试断言技术。
2. Maven Dependencies
2.Maven的依赖性
We need to grab a few dependencies first. In a Maven project, we can add following dependencies to the pom.xml:
我们首先需要抓取一些依赖项。在Maven项目中,我们可以在pom.xml中添加以下依赖项。
JUnit:?
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
</dependency>
<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.11.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency>
3. Using JUnit
3.使用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:
我们将使用isEmpty方法,该方法来自String类和Assert类,以验证给定的String是否为空。由于isEmpty方法在输入String为空时返回真,我们可以将其与assertFalse方法一起使用。
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:
由于text可能是空的,另一种方法是通过使用assertNotEquals方法来进行平等检查。
assertNotEquals("", text);
Or:
或者。
assertNotSame("", text);
Check out our in-depth guide on JUnit assertions here.
请查看我们关于 JUnit 断言的深入指南这里。
All these assertions, when failed, will return an AssertionError.
所有这些断言,一旦失败,将返回AssertionError.。
4. Using Hamcrest Core
4.使用Hamcrest Core
Hamcrest is a well-known framework providing matchers that are commonly used for unit testing in the Java ecosystem.
Hamcrest是一个著名的框架,提供匹配器,通常用于Java生态系统中的单元测试。
We can make use of the Hamcrest CoreMatchers class for empty String checking:
我们可以利用Hamcrest的CoreMatchers类进行空字符串检查。
assertThat(text, CoreMatchers.not(isEmptyString()));
The isEmptyString method is available in the IsEmptyString class.
isEmptyString方法在IsEmptyString类中可用。
This also returns an AssertionError when failing, but with a more helpful output:
当失败时,这也会返回一个AssertionError,但有一个更有用的输出。
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:
如果需要,为了验证一个字符串是否为空或空,我们可以使用isEmptyOrNullString。
assertThat(text, CoreMatchers.not(isEmptyOrNullString()));
To learn about other methods of the CoreMatchers class read this previously published article.
要了解CoreMatchers类的其他方法,请阅读这个以前发表的文章。
5. Using Apache Commons Lang
5.使用Apache Commons Lang
The Apache Commons Lang library provides a host of helper utilities for the java.lang API.
Apache Commons Lang库为java.lang API提供了大量的辅助工具。
The StringUtils class offers a method that we can use to check for empty Strings:
StringUtils类提供了一个方法,我们可以用它来检查空字符串。
assertTrue(StringUtils.isNotBlank(text));
When failing, this returns a simple AssertionError.
当失败时,这将返回一个简单的AssertionError.。
To learn more about String processing with Apache Commons Lang read this article.
要了解更多关于用Apache Commons Lang处理字符串的信息,请阅读这篇文章。
6. Using AssertJ
6.使用AssertJ[/strong
AssertJ is an open source, a community-driven library used for writing fluent and rich assertions in Java tests.
AssertJ是一个开源的、由社区驱动的库,用于在Java测试中编写流畅而丰富的断言。
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:
方法AbstractCharSequenceAssert.isNotEmpty()验证实际的CharSequence不是空的,或者说,换句话说,它不是空的,并且长度为1或更多。
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.
我们有一篇关于AssertJ的介绍性文章这里。
7. Using Google Guava
7.使用Google Guava
Guava is a set of core libraries offered by Google.
Guava是由Google提供的一套核心库。
The method isNullOrEmpty from the Guava Strings class can be utilized to verify if a String is empty (or null):
Guava Strings类中的方法isNullOrEmpty可以用来验证一个字符串是否为空(或空)。
assertFalse(Strings.isNullOrEmpty(text));
This also returns an AssertionError when failing with no other output message.
当失败时,也会返回一个AssertionError,并且没有其他输出信息。
To explore our other articles on the Guava API follow the link here.
要探索我们关于Guava API的其他文章,请点击链接这里。
8. Conclusion
8.结论
In this quick tutorial, we found out how to assert if a given String is empty or not.
在这个快速教程中,我们发现了如何断言一个给定的String是否为空。
As always the full code snippet is available in over on GitHub.
像往常一样,完整的代码片段可在GitHub上找到。