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

Attualmente affermando come questocome verificare Mappa dimensioni utilizzando Hamcrest

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

Ci sono altri metodi come quello utilizzato con gli elenchi.

assertThat (someListReferenceVariable, hasSize (1));

+0

davvero più una domanda Hamcrest che una domanda Mockito. Quindi puoi controllare la documentazione di Hamcrest. Se l'avversario che vuoi non è lì (e credo che non lo sia), potresti sempre scrivere il tuo. –

+1

Utilizzare il metodo entryset() e quindi è possibile verificare con la dimensione. – Bhokal

risposta

9

La buona notizia

C'è un matcher che fa esattamente quello che vuoi nella corrente master branch of the JavaHamcrest project. Si può chiamare in questo modo:

assertThat(mapMap, aMapWithSize(1)); 

E la cattiva notizia

Purtroppo questo matcher non è nella versione più recente di Hamcrest (1.3).

4

non c'è nessuno in Hamcrest 1.3, ma si può facilmente creare il proprio:

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); 
    } 

} 

Testing:

Map<String, Integer> map = new HashMap<>(); 
    map.put("key", 1); 
    assertThat(map, isMapWithSize(1)); 
    assertThat(map, isMapWithSize(equalTo(1))); 
Problemi correlati