0

I have an API :

@RequestMapping(value = "/area/create",
                method = RequestMethod.POST,
                produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<AreaEntry> createNewAreaEntry(
            @RequestBody AreaRequestDto areaRequestDto) throws ServiceException {
    UserContextHolder.setCustomer(areaRequestDto.getAreaEntry().getHost());
    UserContextHolder.setSsoId(areaRequestDto.getUser());
    AreaEntry newAreaEntryDto = service.createNewAreaEntry(areaRequestDto.getAreaEntry());
    System.out.println("The area entries" + areaRequestDto.getUser()
                       + "  " + " " 
                       + areaRequestDto.getAreaEntry().getName() 
                       + "  " 
                       + areaRequestDto.getAreaEntry().getCode() + " deugging");
    System.out.println("the new area entry dto is" + newAreaEntryDto);
    return new ResponseEntity<AreaEntry>(newAreaEntryDto, HttpStatus.OK);

I need to write a JUnit:

private List<String> getInvalidAreas(String areas) throws Exception {
    ResultActions actions = mockMvc.perform(get("/area/create").param("cities", areas));
}

Where I need to pass the class arearequest dto in params How do i do it?

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • are you looking for this ? https://stackoverflow.com/questions/20504399/testing-springs-requestbody-using-spring-mockmvc – Ryuzaki L Dec 16 '19 at 08:11

1 Answers1

0

You can simply pass json string, it will automatically deserialize and convert to DTO. Or you can create utility method that convert your dto object to json and pass it as request body.

 public String asJsonString(final Object obj) throws JsonProcessingException {
        final ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        return mapper.writeValueAsString(obj);
    }