Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

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

Open
mhnagaoka opened this issue Apr 22, 2018 · 0 comments

Comments

@mhnagaoka
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant