Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert that the list is not empty - with or without Hamcrest?

This is related to Checking that a List is not empty in Hamcrest

I have a question over that - If we can assert the list is not empty without using Hamcrest and just using JUnit as:

assertFalse(list.isEmpty());

Is using

assertThat(list.isEmpty(), is(false));

or

assertThat((Collection)list, is(not(empty())));

Worthwhile?

I'm not able to understand are we gaining something using Hamcrest version in this case? Are both equivalent?

like image 398
unknown_boundaries Avatar asked Jan 29 '14 14:01

unknown_boundaries


People also ask

How do I check if Assert is empty?

We'll use the isEmpty method from the String class along with the Assert class from JUnit to verify whether a given String isn't empty. Since the isEmpty method returns true if the input String is empty we can use it together with the assertFalse method: assertFalse(text.

How do you Assert an empty set in Java?

Set isEmpty() method in Java with Examples isEmpty() method is used to check if a Set is empty or not. It returns True if the Set is empty otherwise it returns False. Return Value: The method returns True if the set is empty else returns False.

Which method of Assert checks that a string is empty?

Java String isEmpty() Method The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.


1 Answers

They are the same functionality. The hamcrest provides a more English-like readable language and better error messages. In simple cases like this, I would probably just use the assertFalse

like image 162
Jeff Storey Avatar answered Sep 27 '22 20:09

Jeff Storey