gpt4 book ai didi

spring-mvc - 使用JSONPath和Spring MVC声明数组的数组

转载 作者:行者123 更新时间:2023-12-03 20:23:14 25 4
gpt4 key购买 nike

我很难弄清楚如何在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com