SAP-archive / cloud-bulletinboard-ads

This is the bulletinboard-ads sample application code used in the openSAP course: Cloud-Native Development with SAP Business Technology Platform (formerly SAP Cloud Platform).
Apache License 2.0
76 stars 99 forks source link

Exercise 4 Part 2 `AdvertisementControllerTest.readAll()` unit test verification is not checking the `value` array #6

Open mhnagaoka opened 6 years ago

mhnagaoka commented 6 years ago

In exercise 4, part 2, I changed the AdvertisementControllerTest.readAll() test a little bit:

    @Test
    public void readAll() throws Exception {

        // I changed this block to create 15 ads instead of just 1
        final int adCount = 15;
        for (int i = 0; i < adCount; i++) {
            mockMvc.perform(buildPostRequest(SOME_TITLE + "i"))
                .andExpect(status().isCreated());            
        }

        mockMvc.perform(buildGetRequest(""))
            .andExpect(status().isOk())
            .andExpect(content().contentType(APPLICATION_JSON_UTF8))
            .andExpect(jsonPath("$.length()", is(both(greaterThan(0)).and(lessThan(10)))));
    }

Given that I'm creating 15 ads, I was expecting the test to fail, but it didn't. It seems that the JSON path verification line should be changed from:

.andExpect(jsonPath("$.length()", is(both(greaterThan(0)).and(lessThan(10)))));

to:

.andExpect(jsonPath("$.value.length()", is(greaterThanOrEqualTo(adCount))));

to count the number of elements of the value field in response JSON object.