How can i check if each object in an array has a specific property exists using spring junit integration test?

635 Views Asked by At

I am writing a spring integration test for a method in my resource class . accessing the resource method returns a json response . I would like do an assertion .

the following is my test method.

@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());

}

the response is as follows

{"histogramDistributionbyCdrStatuses":[{"dateRange":"2019-09","total":19,"delivered":7,"undeliverable":4,"expired":4,"enroute":4}]}

the assertion i want to do is each object in the array histogramDistributionbyCdrStatuses has field dateRange, total , delivered , undeliverable , expired and enroute exists. how can i do it . I am also ok to use hamcrest matchers.

really appreciate any help

1

There are 1 best solutions below

0
KnowledgeSeeker001 On BEST ANSWER

I just extended my test as follow and it works

@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());


    }