我很难弄清楚如何在spring mvc中的JSON文档响应中使用jsonPath断言。也许有比使用jsonPath更好的方法来完成这个特定的场景。我想验证链接数组是否具有“self”的rel项,而“self”对象的“href”属性也具有等于“/”的“href”属性。 JSON响应如下所示:
{
"links":[
{
"rel":[
"self"
],
"href":"/"
},
{
"rel":[
"next"
],
"href":"/1"
}
]
}
我试过这个,我可以看到它有rel [0]有自己,但我宁愿不依赖于链接数组和自我的rel数组,并实际测试链接中的href是什么[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")));
}