我很难弄清楚如何在spring mvc中的JSON文档响应中使用jsonPath断言。也许有比在此特定方案中使用jsonPath更好的方法来完成此操作。我想验证links数组是否具有“ self”的相关项,并且“ self”的对象的“ href”属性也具有等于“ /”的“ href”属性。 JSON响应如下所示:
{
"links":[
{
"rel":[
"self"
],
"href":"/"
},
{
"rel":[
"next"
],
"href":"/1"
}
]
}
我尝试了一下,可以看到它具有rel [0]拥有self,但是我宁愿不依赖于self所在的links数组和rel数组中的哪个位置,而是实际测试hrefs在links [rel] [self]上的含义是“ /”。
有任何想法吗?
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(welcomeController).build();
}
@Test
public void givenRootUrl_thenReturnLinkToSelf() throws Exception {
mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.links[0].rel[0].", is("self")));
}
最佳答案
如何添加几个andExpect方法?类似于:
mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.links[0].rel[0]", is("self")))
.andExpect(jsonPath("$.links[0].href[0]", is("/"));
关于spring-mvc - 使用JSONPath和Spring MVC声明数组的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25614593/