java - Assert that collection "Contains at least one non-null element" -
i want verify collection contains @ least 1 non-null element. have tried is(not(empty()))
, passes in 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 elegant/simple way this?
things 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" @ org.hamcrest.matcherassert.assertthat(matcherassert.java:20)
import static org.hamcrest.matcherassert.assertthat; import static org.hamcrest.matchers.*; ... assertthat(collection, hasitem(notnullvalue(integer.class)));
unfortunately, there bug in java 1.6 means might have split onto 2 lines described here if using 1.6:
matcher<iterable<? super string>> matcher = hasitem(notnullvalue(integer.class)); assertthat(collection, matcher);
edit here fest assert example asked for:
import static org.fest.assertions.api.assertions.assertthat; ... assertthat(collection).doesnotcontainnull();
fest requires single static import full ide auto completion.
Comments
Post a Comment