How to assertThat String is not empty
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));
Armine
Updated on July 09, 2022Comments
-
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 almost 7 yearsThis is something like which can be implemented by me. Is that mean, there's no other way using Mathcers?
-
Jan B. almost 7 yearsIt'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 aassertPasswordOk(password)
method that checks all criteria for a valid password. -
Wyzard almost 7 yearsI'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 almost 7 yearsYeah, but yours is better (or at least, it's what I'd actually use, now that I know it exists).
-
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 over 6 yearsGood 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 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 oremptyOrNullString()
in 2.0. -
Armine about 6 yearsGreat solution too!!! I'll take this into consideration for next cases.
-
Catalin about 6 yearsIf the String is null it would throw NullPointerException.
-
Armine over 5 yearsThanks 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 over 5 yearsAs OP didn't mention the "blank" case, entpnerd's answer is more relevant.
-
Tristan over 5 yearsIs 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. about 5 yearsIt's not a good solution for tests because you will not see the value when it fails.
-
entpnerd about 5 yearsI 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
arenull
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. about 5 yearsAh, yeah, actually I found this question while looking for the opposite,
isEmpty
. -
observer over 4 yearsAssertJ Core site has moved to assertj.github.io/doc
-
observer over 4 yearsThat 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 over 2 yearsNOT CORRECT:
assertNotNull
andassertNull
do not verify whether the String is empty (""
); they only check whether it's null.