Java 如何断言字符串不为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44086860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:19:07  来源:igfitidea点击:

How to assertThat String is not empty

javaunit-testingjunithamcrest

提问by Armine

Asserting that a stringis not empty in junit can be done in the following ways:

string可以通过以下方式断言junit中的 a不为空:

 assertTrue(!string.isEmpty());
 assertFalse(string.isEmpty());
 assertThat(string.toCharArray(), is(not(emptyArray())); // (although this didn't compile)

My question is: is there a better way of checking this - something like:

我的问题是:有没有更好的方法来检查这个——比如:

assertThat(string, is(not(empty()))?

assertThat(string, is(not(empty()))?

采纳答案by holi-java

In hamcrest 1.3 you can using Matchers#isEmptyString:

在 hamcrest 1.3 中,您可以使用Matchers#isEmptyString

assertThat(string, not(isEmptyString()));

In hamcrest 2.0 you can using Matchers#emptyString:

在 hamcrest 2.0 中,您可以使用Matchers#emptyString

assertThat(string, is(not(emptyString())));

UPDATE- Notice that : "Maven central has some extra artifacts called java-hamcrest and hamcrest-java, with a version of 2.0.0.0. Please do not use these, as they are an aborted effort at repackaging the different jars." source : hamcrest.org/JavaHamcrest/distributables

更新- 请注意:“Maven 中心有一些额外的工件,称为 java-hamcrest 和 hamcrest-java,版本为 2.0.0.0。请不要使用这些,因为它们是重新打包不同 jar 的失败努力。” 来源:hamcrest.org/JavaHamcrest/distributables

回答by Matt

Write your own TestHelper class where you can collect custom methods for assertions, e.g.

编写您自己的 TestHelper 类,您可以在其中收集断言的自定义方法,例如

 public static void assertEmpty(String input) {...}

回答by Simulant

What you want to assert is the size of your string.

您要断言的是字符串的大小。

assertThat("String is empty",
       string.length(),
       greaterThan(0));

回答by Tomasz Bawor

What you may also do is use library called AssertJwhich provides great fluent assertions into your code. Check can be done with elegant:

您还可以使用名为AssertJ 的库,它为您的代码提供流畅的断言。检查可以用优雅的方式完成:

assertThat(myString).isNotEmpty();

assertThat(myString).isNotEmpty();

回答by Wyzard

I'd use assertThat(string, is(not(equalTo("")))). Unlike other approaches that involve checking the result of the string's .length()or .isEmpty()methods, this will show you the string's contents in the error message when the test fails.

我会用assertThat(string, is(not(equalTo("")))). 与涉及检查字符串.length().isEmpty()方法结果的其他方法不同,当测试失败时,这将在错误消息中向您显示字符串的内容。

(Edit:Actually, no, I wouldn't. I'd use the emptyString()or isEmptyString()matcher as explained in holi-java's answer. Upvote that one, not this one.)

编辑:实际上,不,我不会。我会使用emptyString()orisEmptyString()匹配器,如holi -java 的回答中所述。赞成那个,而不是这个。)

回答by Stefan Birkner

You can use JUnit's own assertNotEqualsassertion:

您可以使用 JUnit 自己的assertNotEquals断言:

Assert.assertNotEquals( "", string );

回答by Russ Hymanson

If you're already using commons-lang3you can do this, which also checks for null and whitespace:

如果您已经在使用commons-lang3,您可以这样做,它还会检查空值和空格:

assertTrue(StringUtils.isNotBlank(string));

As described in their javadocs:

如他们的 javadoc 中所述:

isNotBlank : Checks if a CharSequence is not empty (""), not null and not whitespace only.

isNotBlank :检查 CharSequence 是否不为空(“”)、不为空且不为空白。

回答by Saikat

You can use the Google GuavaLibrary method Strings.isNullOrEmpty

您可以使用谷歌番石榴库方法Strings.isNullOrEmpty

From the JavaDoc

来自 JavaDoc

public static boolean isNullOrEmpty(@Nullable String string)

Returns true if the given string is null or is the empty string.

Consider normalizing your string references with nullToEmpty(java.lang.String). If you do, you can use String.isEmpty() instead of this method, and you won't need special null-safe forms of methods like String.toUpperCase(java.util.Locale) either. Or, if you'd like to normalize "in the other direction," converting empty strings to null, you can use emptyToNull(java.lang.String).

Parameters:

string - a string reference to check

Returns:

true if the string is null or is the empty string

public static boolean isNullOrEmpty(@Nullable String string)

如果给定的字符串为 null 或为空字符串,则返回 true。

考虑使用 nullToEmpty(java.lang.String) 规范化您的字符串引用。如果这样做,您可以使用 String.isEmpty() 代替此方法,并且您也不需要像 String.toUpperCase(java.util.Locale) 这样的特殊空安全形式的方法。或者,如果您想“在另一个方向”规范化,将空字符串转换为 null,您可以使用 emptyToNull(java.lang.String)。

参数

string - 要检查的字符串引用

回报

如果字符串为 null 或为空字符串,则为 true

回答by entpnerd

Consider using Apache's StringUtils.isNotEmpty()method, which is a null-safe check for an empty string.

考虑使用 Apache 的StringUtils.isNotEmpty()方法,这是对空字符串的空安全检查。

assertTrue(StringUtils.isNotEmpty(str));

回答by ?smail Yavuz

Without hamcrest:

无腿甲:

    assertFalse(StringUtils.isEmpty(string));