2013-08-28 5 views
7

मैं यह सत्यापित करना चाहता हूं कि संग्रह में कम से कम एक गैर-शून्य तत्व शामिल है। मैंने 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) 

उत्तर

6
import static org.hamcrest.MatcherAssert.assertThat; 
import static org.hamcrest.Matchers.*; 

... 

assertThat(collection, hasItem(notNullValue(Integer.class))); 

दुर्भाग्य से, वहाँ एक bug in Java 1.6 मतलब है कि आप के रूप में वर्णित here 2 लाइनों पर इसे विभाजित करने के लिए हो सकता है अगर आप 1.6 का उपयोग कर रहे है उदाहरण के लिए आपने पूछा:

import static org.fest.assertions.api.Assertions.assertThat; 
... 
assertThat(collection).doesNotContainNull(); 

फेस्ट को केवल एक ही स्थिर आयात की आवश्यकता है ताकि आपको पूर्ण आईडीई ऑटो पूर्णता मिल सके।

0

कोई आसान तरीका नहीं है। आपको तब तक तत्वों की जांच करनी होगी जब तक कि आपको वह न मिल जाए जो शून्य नहीं है।

public boolean hasAtLeastOneNotNull(Collection<?> collection) { 
    Iterator<?> it = collection.iterator(); 
    while(it.hasNext()) { 
     if (it.next() != null) 
      return true; 
    } 
    return false; 
} 
+1

डाउनवोट क्यों? –

+0

अब अच्छा लग रहा है, हालांकि आपको पहले 'if' की आवश्यकता नहीं है। डाउनवोट मुझसे नहीं है। –

1

आप निम्न की कोशिश कर सकते हैं: अगर आप असंशोधित अपने सरणी छोड़ करना चाहते हैं

public void shouldTestThis() { 
     Collection<Integer> collection = new ArrayList<Integer>(); 
     collection.add(null); 
     collection.removeAll(Collections.singleton(null)); // remove all "null" elements from collection 
     assertThat(collection, is(not(empty()))); 
    } 

अजब नोटिस के रूप में, आप एक इटरेटर का उपयोग करें और संग्रह या एक के अंत तक प्रत्येक तत्व की जाँच करनी चाहिए एक शून्य नहीं।

Matcher<Iterable<? super String>> matcher = hasItem(notNullValue(Integer.class)); 
assertThat(collection, matcher); 

संपादित यहाँ FEST Assert है:

+0

बिंदु यह जांचना है कि इसमें 'शून्य' तत्व हैं या नहीं। आपको उन्हें हटा नहीं देना चाहिए। –

+1

बिंदु "यह सत्यापित करने के लिए है कि एक संग्रह में कम से कम एक गैर-शून्य तत्व होता है।" – Julien

+1

आप संग्रह की एक प्रति बनाकर 'शून्य' तत्वों को हटाए बिना यह कार्य कर सकते हैं (अस्थायी संग्रह 'c' घोषित करें और' c.addAll' का उपयोग करें)। लेकिन उस काम की मात्रा के लिए, आप एक इटरेटर का उपयोग भी कर सकते हैं। – ajb

1

मैं बस एक ही समस्या में भाग गया और इसे निम्नानुसार हल किया।

मूल विचार यह है कि यदि संग्रह में केवल null तत्व हैं, तो एक सेट में परिवर्तित होने पर इसमें केवल एक तत्व होगा और यह null होगा। यदि ऐसा नहीं है, तो संग्रह में कम से कम एक गैर-शून्य तत्व होता है।

मैं एक मिलान करने वाले ने लिखा है, और इस परीक्षण के साथ इसे करने की कोशिश:

import org.hamcrest.Description; 
import org.hamcrest.Factory; 
import org.hamcrest.Matcher; 
import org.hamcrest.TypeSafeMatcher; 
import org.junit.Test; 

import java.util.ArrayList; 
import java.util.Collection; 
import java.util.HashSet; 
import java.util.Set; 

import static org.hamcrest.CoreMatchers.is; 
import static org.hamcrest.CoreMatchers.not; 
import static org.junit.Assert.assertThat; 
import static personal.CollectionOfNullsMatcher.collectionOfNulls; 


public class SimpleTest { 

    @Test 
    public void should_check_collection_for_non_null_values() { 
     Collection<String> testedCollection = new ArrayList<String>(); 
     testedCollection.add(null); 

     assertThat(testedCollection, is(collectionOfNulls())); 

     testedCollection.add("any"); 

     assertThat(testedCollection, is(not(collectionOfNulls()))); 
    } 
} 

class CollectionOfNullsMatcher extends TypeSafeMatcher<Collection> { 

    @Override 
    protected boolean matchesSafely(final Collection collection) { 
     Set<Object> set = new HashSet<Object>(collection); 
     return (set.size() == 1) && (set.toArray()[0] == null); 
    } 

    @Override 
    public void describeTo(final Description description) { 
     description.appendText("collection of nulls"); 
    } 

    @Factory 
    public static <T> Matcher<Collection> collectionOfNulls() { 
     return new CollectionOfNullsMatcher(); 
    } 
} 
बेशक

, एक वास्तविक परियोजना में, मिलान अपने भाइयों :)

आशा है कि यह मदद करता है के साथ एक साथ रखा जाना चाहिए।

+0

नाइस के रूप में उपयोग करना होगा। –

संबंधित मुद्दे