java - Junit java.lang.AssertionError:JSON 路径“$.reward”
问题描述
我有一个提供的测试方法,
@Test
public void calculateReward() throws Exception {
when(userService.findById(any(Long.class))).thenReturn(Optional.of(user));
int steps = 1000;
user.setCurrentSteps(steps);
user.setTotalSteps(steps);
when(userService.save(any(User.class))).thenReturn(user);
Map<String, Double> map = new HashMap<>();
map.put("EUR", 1.0);
when(currencyUtilities.getCurrencyMap()).thenReturn(map);
mockMvc.perform(put("/api/v1/users/calculateReward")
.param("userId", String.valueOf(user.getId())))
.andExpect(
status().isCreated()
).andExpect(
content().contentType(MediaType.APPLICATION_JSON_UTF8)
).andDo(print())
.andExpect(
jsonPath("$.name", is(user.getName()))
).andExpect(
jsonPath("$.currency", is(user.getCurrencyName()))
).andExpect(
jsonPath("$.reward", is(1.0)));
}
我收到错误消息,
java.lang.AssertionError: JSON path "$.reward"
Expected: is <1.0>
but: was "1.00"
Expected :is <1.0>
Actual :"1.00"
这里有什么问题?
解决方案
正如错误消息所说:测试期望在它接收到的 JSON 中看到数字 1.0 ( is(1.0)
),但 JSON 实际上包含该"1.00"
路径中的字符串。阅读https://github.com/json-path/JsonPath了解路径的含义,但$.reward
只是"reward"
根对象的字段。所以它应该看起来像
{
"reward": 1.0,
... other fields including "name" and "currency"
}
但是是
{
"reward": "1.00",
...
}
推荐阅读
- c++ - 如何有效地读取大型且不断增长的文件
- python - 在 Python 中,是否有一种方便的方法来重载 C++ 中的方法?
- python - 无法访问 flask_socketio 应用程序中的 Flask 会话变量
- objective-c - xamarin ios 扩展无法通过 P/Invoke 运行本机代码,而没有“链接全部”链接器行为
- c++ - DXGI 桌面复制,仅捕获特定的 RECT
- html - XRechnung Visualizer 和 Saxon-HE for .NET 的自闭合 DIV 标签问题
- javascript - 从数组 JavaScript 中删除文件
- haskell - 如何使括号免受异步异常的影响?
- sublimetext3 - sublime 自动添加换行符和缩进
- python - Factory-Boy - KeyError: 'locale'