मैं यह सत्यापित करना चाहता हूं कि संग्रह में कम से कम एक गैर-शून्य तत्व शामिल है। मैंने is(not(empty()))
की कोशिश की है, हालांकि यह नीचे दिए गए परीक्षण में गुजरता है।उस संग्रह में "कम से कम एक गैर-शून्य तत्व शामिल है"
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())));
}
}
क्या ऐसा करने के लिए एक सुरुचिपूर्ण/सरल तरीका है?
चीजें जो काम नहीं करते
@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)
डाउनवोट क्यों? –
अब अच्छा लग रहा है, हालांकि आपको पहले 'if' की आवश्यकता नहीं है। डाउनवोट मुझसे नहीं है। –