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