作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想从 json 响应中检索一个值,以便在我的测试用例的其余部分中使用,这是我现在正在做的:
MvcResult mvcResult = super.mockMvc.perform(get("url").accept(MediaType.APPLICATION_JSON).headers(basicAuthHeaders()))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id", is(6))).andReturn();
String responseAsString = mvcResult.getResponse().getContentAsString();
ObjectMapper objectMapper = new ObjectMapper(); // com.fasterxml.jackson.databind.ObjectMapper
MyResponse myResponse = objectMapper.readValue(responseAsString, MyResponse.class);
if(myResponse.getName().equals("name")) {
//
//
}
我想知道是否有更优雅的方法可以直接从
MvcResult
检索值,就像
jsonPath
进行匹配一样?
最佳答案
我找到了一种使用 Jayway 的 JsonPath
的更优雅的方法:
MvcResult mvcResult = super.mockMvc.perform(get("url").accept(MediaType.APPLICATION_JSON).headers(basicAuthHeaders()))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id", is(6))).andReturn();
String response = mvcResult.getResponse().getContentAsString();
Integer id = JsonPath.parse(response).read("$[0].id");
关于spring - 如何直接从 Spring 测试 MvcResult json 响应中检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53486737/
我在 JUnit Test 中使用 MvcResult 类。 如何获取返回的对象? MvcResult r = this.mockMvc .perform(get("/aaaa
我正在测试 API 并从数据库获取所有数据。我将响应保存为 MvcResult result = mockMvc.perform(..some code..).andReturn(); 我收到一个 j
我想从 json 响应中检索一个值,以便在我的测试用例的其余部分中使用,这是我现在正在做的: MvcResult mvcResult = super.mockMvc.perform(get("url"
我是一名优秀的程序员,十分优秀!