0

我正在为我的资源类中的一个方法编写一个弹簧集成测试。访问资源方法返回一个 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 匹配器。

非常感谢任何帮助

4

1 回答 1

0

我刚刚扩展了我的测试如下并且它有效

@Test
    public void testGetPerformanceCdrStatusesByEnrouteStatus() throws Exception {
        this.restMvc.perform(MockMvcRequestBuilders.get(
                "/api/performance/cdrStatus?startDate=2019-09-01T00:00:00.000Z&endDate=2022-12-31T23:59:59.999Z&frequency=PER_DAY"))
                .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())
                   .andExpect(jsonPath("$.histogramDistributionbyCdrStatuses.[*].dateRange").exists())
                   .andExpect(jsonPath("$.histogramDistributionbyCdrStatuses.[*].total").exists())
                   .andExpect(jsonPath("$.histogramDistributionbyCdrStatuses.[*].delivered").exists())
                   .andExpect(jsonPath("$.histogramDistributionbyCdrStatuses.[*].undeliverable").exists())
                   .andExpect(jsonPath("$.histogramDistributionbyCdrStatuses.[*].expired").exists())
                   .andExpect(jsonPath("$.histogramDistributionbyCdrStatuses.[*].enroute").exists());


    }
于 2020-03-16T22:30:24.650 回答