Tôi muốn xác minh rằng bộ sưu tập có chứa ít nhất một phần tử không null. Tôi đã thử is(not(empty()))
, tuy nhiên điều này vượt qua trong bài kiểm tra dưới đây.Xác nhận rằng bộ sưu tập "Chứa ít nhất một phần tử không 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ó cách nào đơn giản/thanh lịch để thực hiện việc này không?
Những điều đó không làm việc
@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)
Tại sao lưu ý lại? –
có vẻ tốt ngay bây giờ, mặc dù bạn không cần 'if' đầu tiên. Downvote không phải từ tôi. –