我提供了一个测试方法,
@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"这里的问题是什么?
发布于 2019-04-15 00:26:09
正如错误消息所说:测试期望在它收到的JSON (is(1.0))中看到数字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",
...
}https://stackoverflow.com/questions/55677423
复制相似问题