Ich möchte überprüfen, ob eine Auflistung mindestens ein Nicht-Null-Element enthält. Ich habe versucht is(not(empty()))
, aber dies geht in dem Test weiter unten.Aktivieren Sie diese Auflistung "Enthält mindestens ein Nicht-Null-Element"
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())));
}
}
Gibt es eine elegante/einfache Möglichkeit, dies zu tun?
Dinge, die nicht
@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)
Warum der Downvote? –
sieht jetzt gut aus, obwohl Sie das erste 'if' nicht brauchen. Der Downvote ist nicht von mir. –