我正在为我的资源类中的一个方法编写一个弹簧集成测试。访问资源方法返回一个 json 响应。我想做一个断言。
以下是我的测试方法。
@Test
public void testGetPerformanceCdrStatusesByDateRangeAndFrequencyMonthly() throws Exception {
this.restMvc.perform(MockMvcRequestBuilders.get(
"/api/performance/cdrStatus?startDate=2019-09-01T00:00:00.000Z&endDate=2019-09-30T23:59:59.999Z&frequency=PER_MONTH"))
.andDo(print()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.histogramDistributionbyCdrStatuses").exists())
.andExpect(jsonPath("$.histogramDistributionbyCdrStatuses").isArray())
.andExpect(jsonPath("$.histogramDistributionbyCdrStatuses").isNotEmpty());
}
响应如下
{"histogramDistributionbyCdrStatuses":[{"dateRange":"2019-09","total":19,"delivered":7,"undeliverable":4,"expired":4,"enroute":4}]}
我想要做的断言是数组 histogramDistributionbyCdrStatuses 中的每个对象都有字段 dateRange、total、delivered、undeliverable、expired 和 enroute 存在。我该怎么做 。我也可以使用 hamcrest 匹配器。
非常感谢任何帮助