如何从 MockMvc 中的 jsonPath() 检索字符串
How to retrieve String from jsonPath() in MockMvc
我想比较两个 jsonPath 值是否相等,如下所示:
this.mockMvc.perform(get(requestURL)).andExpect(jsonPath("$.prop1", equalTo(jsonPath("$.prop2"))));
但后来我的测试失败了。 jsonPath("$.prop1") 返回了我想要的正确值,但是 jsonPath(" $.prop2") 返回的不是这个 属性 的值,而是像这样的类名:
org.springframework.test.web.servlet.result.JsonPathResultMatchers@7b7aaaf6
谁能告诉我如何为 jsonPath() 执行 toString() 方法?我也尝试过 jsonPath("$.prop2").toString() 但也收到了类名。
在此先感谢您!
MvcResult result = this.mockMvc.perform(get(requestURL)).andReturn();
String response = result.getResponse().getContentAsString();
assertEquals(JsonPath.parse(response).read("$.prop1").toString(),JsonPath.parse(response).read("$.prop2").toString());
有关详细信息,请参阅 github readme。
我想比较两个 jsonPath 值是否相等,如下所示:
this.mockMvc.perform(get(requestURL)).andExpect(jsonPath("$.prop1", equalTo(jsonPath("$.prop2"))));
但后来我的测试失败了。 jsonPath("$.prop1") 返回了我想要的正确值,但是 jsonPath(" $.prop2") 返回的不是这个 属性 的值,而是像这样的类名:
org.springframework.test.web.servlet.result.JsonPathResultMatchers@7b7aaaf6
谁能告诉我如何为 jsonPath() 执行 toString() 方法?我也尝试过 jsonPath("$.prop2").toString() 但也收到了类名。
在此先感谢您!
MvcResult result = this.mockMvc.perform(get(requestURL)).andReturn();
String response = result.getResponse().getContentAsString();
assertEquals(JsonPath.parse(response).read("$.prop1").toString(),JsonPath.parse(response).read("$.prop2").toString());
有关详细信息,请参阅 github readme。