lundi 23 octobre 2017

MockMVC in junit tests - checking result for List

I have problems with mockMVC and test written with that. My tests fails. I have two tests methods:

@Test
public void getPersonsForApiConsumerTest() throws Exception {
    mockMvc.perform(get(getUri("/consumers/1/persons")))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$", hasSize(2)))
            .andExpect(jsonPath("$[1].name", is("Ligza")))
            .andExpect(jsonPath("$[2].name", is("Vekrir")));

}

@Test
public void getPersonsForApiConsumerMapTest() throws Exception {
    mockMvc.perform(get(getUri("/consumers/1/persons/map")))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$[1].name", is("Verkir")))
            .andExpect(jsonPath("$[2].name", is("Ligza")));
}

Rest methods with mapping:

 @RequestMapping(value = "/{consumerId}/persons")
    public List<PersonDto> getPersonsForApiConsumer(@PathVariable("consumerId") Integer consumerId) throws IOException {
        return apiConsumerService.getPersonsListForApiConsumer(consumerId);
    }

    @RequestMapping(value = "/{consumerId}/persons/map")
    public Map<ApiConsumerPersonRole, List<Person>> getPersonsForApiConsumerMap(@PathVariable("consumerId") Integer consumerId) throws IOException {
        return apiConsumerService.getPersonsMapForApiConsumer(consumerId);
    }

By Postman I can gain data such:

for list:

[
    {
        "id": 1,
        "firstName": "Ligza",
        "lastName": "Zetasor",
        "role": "REPRESENTATIVE",
        "phone": "123123123",
        "email": "ligza.zetasor@gmail.com"
    },
    {
        "id": 4,
        "firstName": "Vekrir",
        "lastName": "Omegaram",
        "role": "SECURITY_OFFICER",
        "phone": "354654545",
        "email": "vekrir.omegaram@gmail.com"
    }
]

for map:

{
    "SECURITY_OFFICER": [
        {
            "id": 4,
            "firstName": "Vekrir",
            "lastName": "Omegaram",
            "telephone": "354654545",
            "email": "vekrir.omegaram@gmail.com"
        }
    ],
    "REPRESENTATIVE": [
        {
            "id": 1,
            "firstName": "Ligza",
            "lastName": "Zetasor",
            "telephone": "123123123",
            "email": "ligza.zetasor@gmail.com"
        }
    ],
    "COMPLAINT_OFFICER": [],
    "LOCAL_REPRESENTATIVE": []
}

I dont have any clue how to write it properly. I want to check the data inside this list and map.

Is it possible to stream it into that collection or check it easily in the way it is wriiten already?

Aucun commentaire:

Enregistrer un commentaire