Questions tagged «jsonpath»

5
用jsonpath计数成员?
使用JsonPath是否可以计算成员数? 使用Spring MVC Test我正在测试生成的控制器 {"foo": "oof", "bar": "rab"} 与 standaloneSetup(new FooController(fooService)).build() .perform(get("/something").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()) .andExpect(jsonPath("$.foo").value("oof")) .andExpect(jsonPath("$.bar").value("rab")); 我想确保生成的json中没有其他成员。希望通过使用jsonPath计数它们。可能吗?也欢迎其他解决方案。

5
如何测试JSON路径是否不包含特定元素,或者是否存在该元素为null?
我一直在为简单的Spring Web应用程序编写一些简单的单元测试例程。当我在资源的getter方法上添加@JsonIgnore批注时,所得的json对象不包含相应的json元素。因此,当我的单元测试例程尝试测试它是否为空(这是我的情况的预期行为,我不希望密码在json对象中可用)时,测试例程会遇到异常: java.lang.AssertionError:JSON路径:$ .password没有值,异常:路径:$ ['password']没有结果 这是我编写的单元测试方法,使用is(nullValue())方法测试“密码”字段: @Test public void getUserThatExists() throws Exception { User user = new User(); user.setId(1L); user.setUsername("zobayer"); user.setPassword("123456"); when(userService.getUserById(1L)).thenReturn(user); mockMvc.perform(get("/users/1")) .andExpect(jsonPath("$.username", is(user.getUsername()))) .andExpect(jsonPath("$.password", is(nullValue()))) .andExpect(jsonPath("$.links[*].href", hasItem(endsWith("/users/1")))) .andExpect(status().isOk()) .andDo(print()); } 我还尝试了jsonPath()。exists(),它得到了类似的异常,指出该路径不存在。我将分享更多的代码片段,以便使整个情况更具可读性。 我正在测试的控制器方法看起来像这样: @RequestMapping(value="/users/{userId}", method= RequestMethod.GET) public ResponseEntity<UserResource> getUser(@PathVariable Long userId) { logger.info("Request arrived for getUser() with params {}", …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.