Spring MockMvc:以任何顺序匹配JSON对象的集合

问题描述 投票:1回答:1

我有一个API端点,当使用GET调用时,会返回正文中的JSON对象数组,如下所示:

[
  {"id": "321", "created": "2019-03-01", "updated": "2019-03-15"},
  {"id": "123", "created": "2019-03-02", "updated": "2019-03-16"}
]

我想用Spring MockMvc测试用例检查身体。该声明目前看起来像这样:

mockMvc.perform(get("/myapi/v1/goodstuff").
  andExpect(status().isOk()).
  andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)).
  andExpect(jsonPath("$.*", isA(ArrayList.class))).
  andExpect(jsonPath("$.*", hasSize(2))).
  andExpect(jsonPath("$[0].id", is("321"))).
  andExpect(jsonPath("$[0].created", is("2019-03-01"))).
  andExpect(jsonPath("$[0].updated*", is("2019-03-15"))).
  andExpect(jsonPath("$[1].id", is("1232"))).
  andExpect(jsonPath("$[1].created", is("2019-03-02"))).
  andExpect(jsonPath("$[1].updated*", is("2019-03-16")));

但是,我的API的实现并不保证返回数组中JSON对象的顺序。如果这是一个字符串数组,我会通过org.hamcrest.collection.IsIterableContainingInAnyOrder<T>.containsInAnyOrder生成的匹配器来解决这个问题。但我看不到任何合适的匹配器,我的情况in their doc,也没有任何线索在描述jsonPath方法in Spring docs

从快速搜索我没有管理发现任何与我的情况有关的事情,或者超出我上面描述的list of strings situation。当然,我可以将JSON对象转换为字符串。

但我想知道,我能否为JSON对象列表解决这个问题,逐个比较每个对象的每个字段(如上面的代码片段所示),但忽略集合中对象的顺序?

更新:Zgurskyi has suggested解决方案,帮助我原来的简化示例。但是,通过现实生活中的实例,还有2个输入:

  • 字段数为10-20而不是3
  • 并非所有匹配器都是普通的is,例如:

(离我原来的代码更近)

mockMvc.perform(get("/myapi/v1/greatstuff").
      andExpect(status().isOk()).
      andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)).
      andExpect(jsonPath("$.*", isA(ArrayList.class))).
      andExpect(jsonPath("$.*", hasSize(2))).
      andExpect(jsonPath("$[0].id", is("321"))).
      andExpect(jsonPath("$[0].did", anything())).
      andExpect(jsonPath("$[0].createdTs", startsWith("2019-03-01"))).
      andExpect(jsonPath("$[0].updatedTs", startsWith("2019-03-15"))).
      andExpect(jsonPath("$[0].name", equalToIgnoringCase("wat"))).
      andExpect(jsonPath("$[0].stringValues", containsInAnyOrder("a","b","c"))).
      andExpect(jsonPath("$[1].id", is("1232"))).
      andExpect(jsonPath("$[1].did", anything())).
      andExpect(jsonPath("$[1].createdTs", startsWith("2019-03-01"))).
      andExpect(jsonPath("$[1].updatedTs", startsWith("2019-03-15"))).
      andExpect(jsonPath("$[1].name", equalToIgnoringCase("taw"))).
      andExpect(jsonPath("$[1].stringValues", containsInAnyOrder("d","e","f"))).
      andReturn();

到目前为止,我似乎无法做任何比实现我自己的matcher类更好的事情。

或者......我可以吗?

java spring spring-mvc hamcrest mockmvc
1个回答
2
投票

您可以断言列表项字段忽略顺序:

.andExpect(jsonPath("$[*].id", containsInAnyOrder("321", "123")))
.andExpect(jsonPath("$[*].created", containsInAnyOrder("2019-03-01", "2019-03-02")))
.andExpect(jsonPath("$[*].updated", containsInAnyOrder("2019-03-15", "2019-03-16")))

另一种方法是检查响应中是否存在特定的列表项:

.andExpect(jsonPath("$.[?(@.id == 123 && @.created == \"2019-03-02\" && @.updated == \"2019-03-16\")]").exists())
.andExpect(jsonPath("$.[?(@.id == 321 && @.created == \"2019-03-01\" && @.updated == \"2019-03-15\")]").exists())

© www.soinside.com 2019 - 2024. All rights reserved.