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

后端 未结 4 1154
清酒与你
清酒与你 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:27

    If you just want to "match" against a particular Map, you can use some of the answers above, or a custom "matcher" that extends Map, or an ArgumentCaptor, like this:

    ArgumentCaptor argumentsCaptured = ArgumentCaptor.forClass(Map.class);
    verify(mock, times(1)).method((Map) argumentsCaptured.capture());
    assert argumentsCaptured.getValue().containsKey("keyname"); 
    // argumentsCaptured.getValue() will be the first Map it called it with.
    

    See also more answers here: Verify object attribute value with mockito

提交回复
热议问题