2016-03-16 12 views
11
Map<Integer, Map<String, String>> mapMap = new HashMap<Integer,Map<String, String>>(); 

का उपयोग कर मानचित्र आकार सत्यापित करने के लिए वर्तमान में यहकैसे Hamcrest

assertThat(mapMap.size(), is(equalTo(1))); 
Or 
assertThat(mapMap.values(), hasSize(1)); 

की तरह जोर देते हुए वहाँ एक जैसे किसी भी अन्य तरीकों सूचियाँ साथ किया जाता है।

assertThat (someListReferenceVariable, hasSize (1));

+0

सच अधिक एक एक Mockito सवाल से Hamcrest सवाल। तो आप Hamcrest दस्तावेज की जांच कर सकते हैं। यदि आप चाहते हैं कि matcher वहाँ नहीं है (और मुझे विश्वास है कि यह नहीं है), तो आप हमेशा अपना खुद का लिख ​​सकते हैं। –

+1

एंट्रीसेट() विधि का उपयोग करें और फिर आप आकार के साथ सत्यापित कर सकते हैं। – Bhokal

उत्तर

9

अच्छी खबर

एक मिलान वास्तव में क्या आप वर्तमान master branch of the JavaHamcrest project में चाहते हैं करता है। आप यह इतना की तरह कॉल कर सकते हैं:

assertThat(mapMap, aMapWithSize(1)); 

और बुरी खबर

दुर्भाग्य से इस मिलान Hamcrest (1.3) की नवीनतम रिलीज में नहीं है।

4

Hamcrest 1.3 में से कोई भी नहीं है, लेकिन आप बहुत आसानी से अपने खुद बना सकते हैं:

public class IsMapWithSize<K, V> extends FeatureMatcher<Map<? extends K, ? extends V>, Integer> { 
    public IsMapWithSize(Matcher<? super Integer> sizeMatcher) { 
     super(sizeMatcher, "a map with size", "map size"); 
    } 

    @Override 
    protected Integer featureValueOf(Map<? extends K, ? extends V> actual) { 
     return actual.size(); 
    } 

    /** 
    * Creates a matcher for {@link java.util.Map}s that matches when the 
    * <code>size()</code> method returns a value that satisfies the specified 
    * matcher. 
    * <p/> 
    * For example: 
    * 
    * <pre> 
    * Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;(); 
    * map.put(&quot;key&quot;, 1); 
    * assertThat(map, isMapWithSize(equalTo(1))); 
    * </pre> 
    * 
    * @param sizeMatcher 
    *   a matcher for the size of an examined {@link java.util.Map} 
    */ 
    @Factory 
    public static <K, V> Matcher<Map<? extends K, ? extends V>> isMapWithSize(Matcher<? super Integer> sizeMatcher) { 
     return new IsMapWithSize<K, V>(sizeMatcher); 
    } 

    /** 
    * Creates a matcher for {@link java.util.Map}s that matches when the 
    * <code>size()</code> method returns a value equal to the specified 
    * <code>size</code>. 
    * <p/> 
    * For example: 
    * 
    * <pre> 
    * Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;(); 
    * map.put(&quot;key&quot;, 1); 
    * assertThat(map, isMapWithSize(1)); 
    * </pre> 
    * 
    * @param size 
    *   the expected size of an examined {@link java.util.Map} 
    */ 
    @Factory 
    public static <K, V> Matcher<Map<? extends K, ? extends V>> isMapWithSize(int size) { 
     Matcher<? super Integer> matcher = equalTo(size); 
     return IsMapWithSize.<K, V> isMapWithSize(matcher); 
    } 

} 

परीक्षण:

Map<String, Integer> map = new HashMap<>(); 
    map.put("key", 1); 
    assertThat(map, isMapWithSize(1)); 
    assertThat(map, isMapWithSize(equalTo(1))); 
संबंधित मुद्दे