How to assertThat String is not empty

64,999

Solution 1

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

Solution 2

You can use JUnit's own assertNotEquals assertion:

Assert.assertNotEquals( "", string );

Solution 3

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

Solution 4

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.

Solution 5

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

assertTrue(StringUtils.isNotEmpty(str));
Share:
64,999
Armine
Author by

Armine

Updated on July 09, 2022

Comments

  • Armine
    Armine almost 2 years

    Asserting that a string is 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 of checking this - something like:

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

  • Armine
    Armine almost 7 years
    This is something like which can be implemented by me. Is that mean, there's no other way using Mathcers?
  • Jan B.
    Jan B. almost 7 years
    It's just a hint. The larger your application grows it is helpful to have custom assertions and helper methods that support testing your own business logic. For instance, instead of testing assertTrue(password.length>6) you could have a assertPasswordOk(password) method that checks all criteria for a valid password.
  • Wyzard
    Wyzard almost 7 years
    I'd figured there ought to be something like that, but I assumed there must not be or the OP wouldn't have asked. Thanks for actually checking. :-)
  • Wyzard
    Wyzard almost 7 years
    Yeah, but yours is better (or at least, it's what I'd actually use, now that I know it exists).
  • holi-java
    holi-java almost 7 years
    @Wyzard yes, it can make the test code more readable (we can read the expressiveness test as an article).
  • Steven Kuypers
    Steven Kuypers over 6 years
    Good answer but beware that the statements return true when the String is null.. Depending on your specs, you might want to add CoreMatchers.notNullValue() to your test.
  • holi-java
    holi-java over 6 years
    @StevenKuypers yes, sir. but the question is check whether String is empty rather than null or empty. for that there are related matchers, e.g: isEmptyOrNullString() in 1.3 or emptyOrNullString() in 2.0.
  • Armine
    Armine about 6 years
    Great solution too!!! I'll take this into consideration for next cases.
  • Catalin
    Catalin about 6 years
    If the String is null it would throw NullPointerException.
  • Armine
    Armine over 5 years
    Thanks for the answer. but my question in not related to that the string can be null, but the way of asserting that string is not empty (I have covered the nullability part, it is out of the scope of this question).
  • amseager
    amseager over 5 years
    As OP didn't mention the "blank" case, entpnerd's answer is more relevant.
  • Tristan
    Tristan over 5 years
    Is there really a "hamcrest 2.0" though ? "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
  • Alex P.
    Alex P. about 5 years
    It's not a good solution for tests because you will not see the value when it fails.
  • entpnerd
    entpnerd about 5 years
    I hear what you're saying but I think that it's a decent solution for tests. I use lines of code like this in my own tests from time to time, and they work well enough.. As for the value, itself, all you're validating is that it isn't empty. Thus, if this assertion fails, the only possible values for str are null and "". In the event that you truly do want to see the value if the test fails, you can always use the slightly different version of the above line of code: assertTrue("String was empty. str=" + str, StringUtils.isNotEmpty(str));.
  • Alex P.
    Alex P. about 5 years
    Ah, yeah, actually I found this question while looking for the opposite, isEmpty.
  • observer
    observer over 4 years
    AssertJ Core site has moved to assertj.github.io/doc
  • observer
    observer over 4 years
    That approach is fine for things that are domain specific. For such generic things like asserting emptiness, refer to libraries like AssertJ, as suggested by @Tomasz Bawor.
  • Paulo Merson
    Paulo Merson over 2 years
    NOT CORRECT: assertNotNull and assertNull do not verify whether the String is empty (""); they only check whether it's null.