gpt4 book ai didi

java - Junit java.lang.AssertionError : JSON path "$.reward"

转载 作者:行者123 更新时间:2023-12-02 10:00:21 29 4
gpt4 key购买 nike

我提供了一个测试方法,

@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",
...
}

关于java - Junit java.lang.AssertionError : JSON path "$.reward",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55677423/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com