java - Spring Boot 中 Rest Client Junit 的 InvalidUseOfMatchersException
问题描述
我正在尝试在 Spring Boot 中使用 mockito 为 Rest Client 编写 Junit 测试用例。在模拟响应时出现错误,如下所示:
使用 Mockito 模拟响应:
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(Url);
Mockito.when(this.restTlsTemplate
.exchange(Mockito.eq(builder.toString()), HttpMethod.GET, Mockito.any(), RestResponse.class)
.getBody()).thenReturn(response());
获取上述模拟响应时出错:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
4 matchers expected, 2 recorded:
-> at (ApplicationTests.java:54)
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"));
任何人都可以检查这个并帮助我。
解决方案
如果您对存根或验证方法调用中的参数之一使用匹配器,例如Mockito.eq
或,则必须对所有参数使用匹配器。Mockito.any
这是因为 Mockito 将其匹配器排列在内部堆栈中的方式。所以你应该写
Mockito.when(this.restTlsTemplate
.exchange(Mockito.eq(builder.toString()), Mockito.eq(HttpMethod.GET), Mockito.any(), Mockito.eq(RestResponse.class))
.getBody()).thenReturn(response());
whereMockito.eq
已放置在您要精确匹配的每个参数周围。
推荐阅读
- gml - 运动速度不在状态机中更新
- javascript - Babel 和 Webpack es6 错误,尝试了许多修复,但无法正常工作
- python - 熊猫:从镶木地板文件中读取前 n 行?
- javascript - NodeJS - 使用 RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING 解密
- java - 在Jenkins中重新部署WAR时出错,如何解决?
- javascript - Udacity:测验:导航食物链 - 三元运算符中的三元运算符
- java - 如何通过对话框从另一个文件中读取文本字段值?
- java - 如何更新 JavaFX?我必须下载新版本的 JDK 吗?
- c# - SqlBulkCopy 字符串 NULL CSV
- c++ - 如何在退出时修复 OpenGL 堆栈损坏