首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从jsonPath()检索MockMvc中的字符串

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

Stack Overflow用户
提问于 2018-03-28 12:02:57
回答 1查看 2.9K关注 0票数 4

如果两个jsonPath值相等,我想比较一下:

代码语言:javascript
复制
this.mockMvc.perform(get(requestURL)).andExpect(jsonPath("$.prop1", equalTo(jsonPath("$.prop2"))));

但后来我的考试失败了。jsonPath("$.prop1")返回我想要的正确值,但是jsonPath("$.prop2")返回的不是这个属性的值,而是类名:

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

有人能告诉我如何为()执行jsonPath() 方法吗?我也尝试过jsonPath("$.prop2").toString(),但也收到了这个类名。

谢谢你的进阶!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-28 14:26:02

代码语言:javascript
复制
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自述

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49534041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档