How to claim that a string is not empty

The statement that a string not empty in junit can be done in the following ways:

  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 to check this: something like:

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

+24
java unit-testing junit hamcrest
source share
10 answers

In Hamcrest 1.3 you can use Matchers # isEmptyString :

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

In Hamcrest 2.0 you can use Matchers # emptyString :

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

UPDATE - Please note: “Maven central has some additional artifacts, called java-hamcrest and hamcrest-java, with version 2.0.0.0. Please do not use them as they are an aborted attempt to repack different banks.” Source: hamcrest.org / JavaHamcrest / distributables

+45
source share

You can use your own assertNotEquals JUnit statement:

 Assert.assertNotEquals( "", string ); 
+5
source share

I would use assertThat(string, is(not(equalTo("")))) . Unlike other approaches, which include checking the results of the string methods .length() or .isEmpty() , this will show you the contents of the string in the error message when the test fails.

( Edit: Actually, no, I would not do this. I used the emptyString() or isEmptyString() as described in holi-java answer . Recall that one, not this one.)

+3
source share

Try using the Apache StringUtils.isNotEmpty () method, which is a null check on an empty string.

 assertTrue(StringUtils.isNotEmpty(str)); 
+3
source share

You can also use the AssertJ library, which provides fast and fast statements in your code. Verification can be done with an elegant:

assertThat(myString).isNotEmpty();

+2
source share

If you are already using commons-lang3 , you can do this, which also checks for null and whitespace characters:

 assertTrue(StringUtils.isNotBlank(string)); 

As described in their javadocs:

isNotBlank: checks if CharSequence is not empty (""), not null and not just a space.

+2
source share

Write your own TestHelper class, where you can collect custom methods for statements, for example.

  public static void assertEmpty(String input) {...} 
+1
source share

You can use the Google library Guava Library method Strings.isNullOrEmpty

From JavaDoc

public static boolean isNullOrEmpty (@Nullable String string)

Returns true if the specified string is zero or is an empty string.

Try normalizing string references with nullToEmpty (java.lang.String). If you do this, you can use String.isEmpty () instead of this method, and you will not need special null safe forms of methods such as String.toUpperCase (java.util.Locale) either. Or, if you want to normalize "in the other direction" by converting empty strings to zero, you can use emptyToNull (java.lang.String).

Parameters :

string - a link to the string to check

Returns :

true if the string is empty or is an empty string

+1
source share

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

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

If you use spring, you can use the StringUtils.hasLength(String str) -from org.springframework.util package- function, which does a zero check and checks the length of a given string.

0
source share

All Articles