加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Ambiguous Mockito – 0匹配预期,1记录(InvalidUseOfMat

发布时间:2020-12-15 02:59:30 所属栏目:Java 来源:网络整理
导读:我面临一个非常奇怪的问题. URL = "/my/specific/url/"; when(this.restHelperMock.post( eq(myEnum),eq(this.config.apiEndpoint() + URL),any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent)); 甚至包含 URL = "/my/specific/url/"
我面临一个非常奇怪的问题.
URL = "/my/specific/url/";
 when(this.restHelperMock.post(
 eq(myEnum),eq(this.config.apiEndpoint() + URL),any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent));

甚至包含

URL = "/my/specific/url/";
 when(this.restHelperMock.post(
 eq(myEnum),contains(this.config.apiEndpoint() + URL),any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent));

给我

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

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(),"raw String");
When using matchers,all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(),eq("String by matcher"));

For more info see javadoc for Matchers class.

即使我不使用RAW表达式.
奇怪的是,如果我将contains方法更改为:

URL = "/my/specific/url/";
 when(this.restHelperMock.post(
 eq(myEnum),contains(URL),any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent));

省略端点,它的工作原理.

Config和RestHelper都被嘲笑:

this.restHelperMock = mock(RESTHelper.class);
this.config = mock(MyBMWConfiguration.class);

when(this.config.apiEndpoint()).thenReturn("http://host:port/api");

ApiEndpoint的URL等于我想要模拟的,
即使它不会,我应该得到一个NullpointerException,因为虚假的嘲弄.
但在这里,我没有任何想法.

谢谢您的回答.

解决方法

问题似乎是你在eq(…)调用期间调用了一个模拟方法this.config.apiEndpoint().尝试简单地将完整的URL放在那里(host:port / api / my / specific / url)而不是在那里调用另一个模拟,这可能会混淆Mockito,因为它依赖于内部状态进行模拟.

说实话,我不是那么深入Mockito,我可以解释为什么会发生这种情况,但有一天我可能会尝试调试它;-)

编辑:奇怪的是,我似乎无法用更简单的测试用例重现它.这里似乎有更多,而不是满足眼睛.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读