java - java.lang.AssertionError:JSON 路径中没有值
问题描述
我运行了一个单元测试,我想在其中获取一个 JSON 对象并需要将它与值进行比较。下面提供了终点,
private static final String TOTAL_REWARD = "total_reward_";
private static final String EUR = "EUR";
@GetMapping(value = "/findUsersPayouts")
public ResponseEntity<String> findUsersPayoutList(@RequestParam("userId") Long userId) {
Optional<User> optional = userService.findById(userId);
if (!optional.isPresent()) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
User user = optional.get();
List<Reward> rewards = user.getRewards();
JSONObject obj = new JSONObject();
obj.put("id", user.getId());
obj.put("name", user.getName());
JSONArray array = new JSONArray();
for (Reward reward : rewards) {
JSONObject currData = new JSONObject();
double amount = reward.getAmount();
String currencyName = user.getCurrencyName();
Map<String, Double> currencyMap = currencyUtilities.getCurrencyMap();
currData.put(TOTAL_REWARD + EUR, amount);
/*
* convert the reward amount to 2 decimals limit
* */
DecimalFormat df = new DecimalFormat("#.00");
double rewardInLocalCurrency = amount * currencyMap.get(currencyName);
String rewd = df.format(rewardInLocalCurrency);
currData.put(TOTAL_REWARD + currencyName, rewd);
array.put(currData);
}
obj.put("rewards", array);
return ResponseEntity.status(HttpStatus.CREATED).contentType(MediaType.APPLICATION_JSON_UTF8).body(obj.toString());
}
我这里有测试,
@Test
public void findUsersPayoutList() throws Exception {
when(userService.findById(any(Long.class))).thenReturn(Optional.of(user));
Reward reward = new Reward();
reward.setUser(user);
reward.setId(55L);
reward.setAmount(1);
Reward reward1 = new Reward();
reward1.setUser(user);
reward1.setId(57L);
reward1.setAmount(1);
user.addReward(reward);
user.addReward(reward1);
Map<String, Double> map = new HashMap<>();
map.put("EUR", 1.0);
when(currencyUtilities.getCurrencyMap()).thenReturn(map);
JSONArray rewards = new JSONArray();
JSONObject object = new JSONObject();
object.put("total_reward_EUR :", 1);
rewards.put(object);
rewards.put(object);
mockMvc.perform(get("/api/v1/users/findUsersPayouts").param("userId", String.valueOf(user.getId())))
.andExpect(
status().isCreated()
).andExpect(
content().contentType(MediaType.APPLICATION_JSON_UTF8)
).andDo(print())
.andExpect(
jsonPath("$.id", is(user.getId().intValue()))
).andExpect(
jsonPath("$.name", is(user.getName()))
).andExpect(
jsonPath("$.rewards[0].['total_reward_EUR :']", is("1.00"))
).andExpect(
jsonPath("$.rewards[1].['total_reward_EUR :']", is("1.00"))
);
}
当我运行测试时,我得到输出,
java.lang.AssertionError:JSON 路径“$.rewards[0].['total_reward_EUR :']”处没有值
我调试并获得所提供的价值,
测试有什么问题?
解决方案
您应该使用它$.rewards[0].['total_reward_EUR']
来访问json 中数组中total_reward_EUR
第一个对象的属性。rewards
推荐阅读
- arrays - vb.net:列出变量而不是值
- python - 'int' 对象不能从数组中调用
- gradle - 我们可以使用 gradle 更改子模块的构建顺序吗?
- javascript - Next.js 正在采取两个完整的 F5 刷新操作来在静态页面上显示新内容,并使用“重新验证:1”?我期待一个
- r - 从 URL 读取图像的 R 函数
- python - 如何为 Django 表格内联添加不同的占位符
- vba - 基于 Outlook 模板,从 Word 菜单创建电子邮件
- javascript - 如何让 HOC 对覆盖道具做出反应?
- google-apps-script - 存档脚本随机停止处理工作表上已填充的数据
- anaconda - Azure ML 无法创建 conda 环境(退出代码:-15)