Assert that the collection "contains at least one non-zero element"

I want to verify that the collection contains at least one nonzero element. I tried is(not(empty())) , however this passes in the test below.

 import org.junit.Test; import java.util.ArrayList; import java.util.Collection; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.not; public class SandBoxTest { @Test public void shouldTestThis() { Collection<Integer> collection = new ArrayList<Integer>(); collection.add(null); assertThat(collection, is(not(empty()))); } } 

Is there an elegant / easy way to do this?

Things that don't work

 @Test public void should(){ Collection<String> collection = new ArrayList(); collection.add("gfas"); collection.add("asda"); assertThat(collection, contains(notNullValue())); } java.lang.AssertionError: Expected: iterable containing [not null] but: Not matched: "asda" at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20) 
+7
java unit-testing junit assertions hamcrest
source share
4 answers
 import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; ... assertThat(collection, hasItem(notNullValue(Integer.class))); 

Unfortunately, there is a bug in Java 1.6 , which means that you may have to split it into 2 lines, as described here if you are using 1.6:

 Matcher<Iterable<? super String>> matcher = hasItem(notNullValue(Integer.class)); assertThat(collection, matcher); 

EDIT Here is an example of the FEST Assert that you requested:

 import static org.fest.assertions.api.Assertions.assertThat; ... assertThat(collection).doesNotContainNull(); 

FEST requires only one static import, so you get the complete completion of the IDE.

+6
source share

You can try the following:

 public void shouldTestThis() { Collection<Integer> collection = new ArrayList<Integer>(); collection.add(null); collection.removeAll(Collections.singleton(null)); // remove all "null" elements from collection assertThat(collection, is(not(empty()))); } 

As ajb notes, if you want to leave your array unmodified, you should use an iterator and check each element to the end of the collection or not null.

+1
source share

I just ran into the same problem and solved it as follows.

The basic idea is that if a collection has only null elements converted to a set, it will contain only one element and will be null . If this is not the case, then the collection contains at least one nonzero element.

I wrote a match and tried it with this test:

 import org.hamcrest.Description; import org.hamcrest.Factory; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; import org.junit.Test; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Set; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.junit.Assert.assertThat; import static personal.CollectionOfNullsMatcher.collectionOfNulls; public class SimpleTest { @Test public void should_check_collection_for_non_null_values() { Collection<String> testedCollection = new ArrayList<String>(); testedCollection.add(null); assertThat(testedCollection, is(collectionOfNulls())); testedCollection.add("any"); assertThat(testedCollection, is(not(collectionOfNulls()))); } } class CollectionOfNullsMatcher extends TypeSafeMatcher<Collection> { @Override protected boolean matchesSafely(final Collection collection) { Set<Object> set = new HashSet<Object>(collection); return (set.size() == 1) && (set.toArray()[0] == null); } @Override public void describeTo(final Description description) { description.appendText("collection of nulls"); } @Factory public static <T> Matcher<Collection> collectionOfNulls() { return new CollectionOfNullsMatcher(); } } 

Of course, in a real project, an assistant should be placed with his brothers :)

Hope this helps.

+1
source share

There is no easy way. You must check the elements until you find a null value.

 public boolean hasAtLeastOneNotNull(Collection<?> collection) { Iterator<?> it = collection.iterator(); while(it.hasNext()) { if (it.next() != null) return true; } return false; } 
0
source share

All Articles