如何从MockMvc中的jsonPath()中检索字符串

sch*_*hau 4 spring mocking jsonpath

我想比较两个jsonPath值是否相等,如下所示:

this.mockMvc.perform(get(requestURL)).andExpect(jsonPath("$.prop1", equalTo(jsonPath("$.prop2"))));
Run Code Online (Sandbox Code Playgroud)

但后来我的测试失败了。该(“ $为prop1”)jsonPath返回正确的价值我想要的,但jsonPath(“$ PROP2。”)不返回该属性的值,而不是像类名:

org.springframework.test.web.servlet.result.JsonPathResultMatchers@7b7aaaf6

谁能给我个主意,我如何执行jsonPath()的toString()方法?我也尝试了jsonPath(“ $。prop2”)。toString(),但也收到了类名。

谢谢高级!

小智 6

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());
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参见github自述文件。