【发布时间】:2020-12-31 16:01:55
【问题描述】:
我在休息端点上运行测试时遇到问题:
java.lang.AssertionError: JSON 路径“$.[*].montant”
预期:包含 的集合 但是:是
预期:包含 的集合 实际:
代码是:
restMockMvc.perform(get("/api/{id}", dommage.getId()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.[*].montant").value(hasItem(dommage.getMontant())));
测试失败,因为值不匹配,但是如何格式化 json 结果以匹配浮点数或测试与当前输出匹配?
感谢您的帮助
【问题讨论】:
-
澄清一下,API 的响应是“1.0”,而您希望它是“1.0F”?我问是因为属性是数字没关系,答案是 1.0
-
没错,API 响应是 1.0 但测试需要 1.0F 所以失败了。
-
可以显示
getMontant()方法的返回类型吗? -
我使用 lombok 注释作为 getter/setter 但属性是:private Float montant
-
您能在“hasItem”方法中尝试类似“new DecimalFormat("#.##").format(dommage.getMontant())”的方法
标签: java spring spring-boot junit5