junit - Mockito 核心匹配器-GreaterThan
问题描述
我正在尝试在我的项目中升级 mockito 版本。
当前版本:mockito-all [1.9.5]
升级到:mockito-core [2.8.8]
我的一些旧测试在这个问题上失败了
测试:
.andExpect(jsonPath("$.paymentTransaction.totalAmount").value(new GreaterThan<Double>(0.0D)))
失败消息:
java.lang.AssertionError: For JSON path $.paymentTransaction.totalAmount type of value
Expected :class org.mockito.internal.matchers.GreaterThan
Actual :class java.lang.Double
任何线索,如果要通过 mockito 版本升级以不同的方式处理?
解决方案
您最终使用了错误的重载:
public ResultMatcher value(Object expectedValue)
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/web/servlet/result/JsonPathResultMatchers.html#value-java.lang.Object-
代替
public <T> ResultMatcher value(Matcher<T> matcher)
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/web/servlet/result/JsonPathResultMatchers.html#value-org.hamcrest.Matcher-
原因是你使用了 Mockito 的 matcher,而不是 Hamcrest 的。你需要使用:
org.hamcrest.Matchers.greaterThan(T value)
另外,请注意,jsonPath
将 Hamcrest 匹配器作为第二个参数存在过载(无需调用value
)
推荐阅读
- python - 在 Visual Studio Code 中运行 Python 时出错:错误:会话无法生成请求
- tomcat9 - 请求的资源 [/ords] 不可用 404 未找到
- visual-studio-code - VSCode - 如何防止抽屉在启动时打开
- php - 在 PHP 中将小写字母“ß”转换为大写字母“ẞ”
- python - 未找到 botocore<1.22.0,>=1.21.0 的匹配分布(来自 awsebcli)
- java - android studio升级后无法运行flutter应用
- javascript - 从 json 文件中读取 ip 电子反应
- sql - 应该是对 YOY SQL Calc 的简单回答
- python - Python 使用 Shareplum 将文件上传到 Sharepoint
- c# - 从转发器中提取数据而无需页面回发