Chcę sprawdzić, czy kolekcja zawiera co najmniej jeden element inny niż zerowy. Próbowałem is(not(empty()))
, jednak to przechodzi w teście poniżej.Należy potwierdzić, że kolekcja "Zawiera co najmniej jeden element inny niż pusty"
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())));
}
}
Czy istnieje elegancki/prosty sposób na zrobienie tego?
rzeczy, które nie działają
@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)
Dlaczego spadamy? –
wygląda teraz dobrze, chociaż nie potrzebujesz pierwszego "jeśli". Zgłoszenie nie jest ode mnie. –