Search code examples
spring-mvcintegration-testingspring-testmockmvcspring-test-mvc

MockMvc Integration Test with List of Object as Request Param


I am working on a REST service using Spring MVC which takes List of Object as request parameter.

    @RequestMapping(value="/test", method=RequestMethod.PUT)
    public String updateActiveStatus(ArrayList<Test> testList, BindingResult result) throws Exception {
        if(testList.isEmpty()) {
            throw new BadRequestException();
        }
        return null;
    }

When I am trying a Integration test for above service, I am not able to send the list of Test object in request param.

Following code is not working for me.

List<Test> testList = Arrays.asList(new Test(), new Test());
        mockMvc.perform(put(ApplicationConstants.UPDATE_ACTIVE_STATUS)
                .content(objectMapper.writeValueAsString(testList)))
            .andDo(print());

Can anyone please help on this!


Solution

  • Use Gson library to convert list into a json string and then put that string in content

    Also put the @RequestBody annotation with the method parameter in the controller

    public String updateActiveStatus(@RequestBody ArrayList<...