Desidero verificare che una raccolta contenga almeno un elemento non null. Ho provato is(not(empty()))
, tuttavia questo passaggio nel test di seguito.Asserire che la raccolta "Contiene almeno un elemento non null"
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())));
}
}
C'è un modo elegante/semplice per farlo?
cose che non funzionano
@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)
Perché il downvote? –
sembra buono ora, anche se non è necessario il primo 'se'. Il downvote non è da parte mia. –