Using Mockito, how do I match against the key-value pair of a map?

后端 未结 4 1153
清酒与你
清酒与你 2021-02-12 11:50

I need to send a specific value from a mock object based on a specific key value.

From the concrete class:

map.put(\"xpath\", \"PRICE\");
search(map);
         


        
4条回答
  •  失恋的感觉
    2021-02-12 12:31

    I found this trying to solve a similar issue creating a Mockito stub with a Map parameter. I didn't want to write a custom matcher for the Map in question and then I found a more elegant solution: use the additional matchers in hamcrest-library with mockito's argThat:

    when(mock.search(argThat(hasEntry("xpath", "PRICE"))).thenReturn("$100.00");
    

    If you need to check against multiple entries then you can use other hamcrest goodies:

    when(mock.search(argThat(allOf(hasEntry("xpath", "PRICE"), hasEntry("otherKey", "otherValue")))).thenReturn("$100.00");
    

    This starts to get long with non-trivial maps, so I ended up extracting methods to collect the entry matchers and stuck them in our TestUtils:

    import static org.hamcrest.Matchers.allOf;
    import static org.hamcrest.Matchers.anyOf;
    import static org.hamcrest.Matchers.hasEntry;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    import org.hamcrest.Matcher;
    ---------------------------------
    public static  Matcher> matchesEntriesIn(Map map) {
        return allOf(buildMatcherArray(map));
    }
    
    public static  Matcher> matchesAnyEntryIn(Map map) {
        return anyOf(buildMatcherArray(map));
    }
    
    @SuppressWarnings("unchecked")
    private static  Matcher>[] buildMatcherArray(Map map) {
        List>> entries = new ArrayList>>();
        for (K key : map.keySet()) {
            entries.add(hasEntry(key, map.get(key)));
        }
        return entries.toArray(new Matcher[entries.size()]);
    }
    

    So I'm left with:

    when(mock.search(argThat(matchesEntriesIn(map))).thenReturn("$100.00");
    when(mock.search(argThat(matchesAnyEntryIn(map))).thenReturn("$100.00");
    

    There's some ugliness associated with the generics and I'm suppressing one warning, but at least it's DRY and hidden away in the TestUtil.

    One last note, beware the embedded hamcrest issues in JUnit 4.10. With Maven, I recommend importing hamcrest-library first and then JUnit 4.11 (now 4.12) and exclude hamcrest-core from JUnit just for good measure:

    
        org.hamcrest
        hamcrest-library
        1.3
        test
    
    
        junit
        junit
        4.12
        test
        
            
                org.hamcrest
                hamcrest-core
            
        
    
    
        org.mockito
        mockito-all
        1.9.5
        test
    
    

    Edit: Sept 1, 2017 - Per some of the comments, I updated my answer to show my Mockito dependency, my imports in the test util, and a junit that is running green as of today:

    import static blah.tool.testutil.TestUtil.matchesAnyEntryIn;
    import static blah.tool.testutil.TestUtil.matchesEntriesIn;
    import static org.hamcrest.MatcherAssert.assertThat;
    import static org.hamcrest.Matchers.is;
    import static org.mockito.Matchers.argThat;
    import static org.mockito.Mockito.mock;
    import static org.mockito.Mockito.when;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.junit.Test;
    
    public class TestUtilTest {
    
        @Test
        public void test() {
            Map expected = new HashMap();
            expected.put(1, "One");
            expected.put(3, "Three");
    
            Map actual = new HashMap();
            actual.put(1, "One");
            actual.put(2, "Two");
    
            assertThat(actual, matchesAnyEntryIn(expected));
    
            expected.remove(3);
            expected.put(2, "Two");
            assertThat(actual, matchesEntriesIn(expected));
        }
    
        @Test
        public void mockitoTest() {
            SystemUnderTest sut = mock(SystemUnderTest.class);
            Map expected = new HashMap();
            expected.put(1, "One");
            expected.put(3, "Three");
    
            Map actual = new HashMap();
            actual.put(1, "One");
    
            when(sut.search(argThat(matchesAnyEntryIn(expected)))).thenReturn("Response");
            assertThat(sut.search(actual), is("Response"));
        }
    
        protected class SystemUnderTest {
            // We don't really care what this does
            public String search(Map map) {
                if (map == null) return null;
                return map.get(0);
            }
        }
    }
    

提交回复
热议问题