java Mockito 匹配任何地图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27646362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:10:35  来源:igfitidea点击:

Mockito Matchers any Map

javatestingmockito

提问by XXLUser

How can I use any map in mockito? I tried with following codes

如何在 mockito 中使用任何地图?我尝试使用以下代码

when(mockedService.patch("1", Matchers.<Map<String, Object>>any())).thenReturn(object);

and with:

与:

when(mockedService.patch("1", anyMap())).thenReturn(object);

But it returns:

但它返回:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded.

It works only when I put any(String.class):

它仅在我放置时有效any(String.class)

when(mockedService.patch(any(String.class), Matchers.<Map<String, Object>>any())).thenReturn(object);

But I want to have option of puting actual values instead of any String

但我想选择放置实际值而不是任何字符串

回答by yshavit

You can't mix matchers and non-matchers. Instead of "1", use Matchers.eq("1"). This creates a matcher that matches any string equal to "1", which satisfies both your needs (equal to "1") and Mockito's (both arguments are matchers).

你不能混合匹配器和非匹配器。而不是"1",使用Matchers.eq("1")。这将创建一个匹配任何等于“1”的字符串的匹配器,它满足您的需求(等于“1”)和 Mockito 的(两个参数都是匹配器)。