How to assertThat String is not empty

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

assertThat(myString).isNotEmpty();


You can use JUnit's own assertNotEquals assertion:

Assert.assertNotEquals( "", string );

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

assertTrue(StringUtils.isNotBlank(string));

As described in their javadocs:

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


In hamcrest 1.3 you can using Matchers#isEmptyString :

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

In hamcrest 2.0 you can using 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