简体   繁体   中英

Rest template doesn't return response entity after exception handler

I have the following controller method

@PostMapping(consumes = "application/json",
             produces = "application/json")
public ResponseEntity<Board> createBoard(@Valid @RequestBody 
BoardRequestDTO boardRequestDTO) {
    Board board = new Board();
    board.setName(boardRequestDTO.getName());
    board.setCompanyId(Integer.valueOf(boardRequestDTO.getCompanyId()));
    board.setCode(boardRequestDTO.getCode());
    Board dbBoard = boardRepository.save(board);
    return new ResponseEntity<>(dbBoard, HttpStatus.CREATED);
} 

And have the following exception heandler for it that handle validation error

@ControllerAdvice
public class ExceptionHandlerConfig extends ResponseEntityExceptionHandler   {
    @Override
    protected ResponseEntity<Object>       
    handleMethodArgumentNotValid(MethodArgumentNotValidException ex, 
       HttpHeaders headers, HttpStatus status, WebRequest request) {
       ValidationErrorResponses validationErrorResponses = new 
       ValidationErrorResponses();
       for (FieldError fieldError : ex.getBindingResult().getFieldErrors())
       validationErrorResponses.addError(new ValidationError(fieldError.getField(),fieldError.getDefaultMessage()));

       ResponseEntity<Object> responseEntity = new ResponseEntity<Object> 
       (validationErrorResponses, HttpStatus.UNPROCESSABLE_ENTITY);
       return responseEntity;
    }

 }

When I send fail request via Postman I get the predictable response:

{
    "errors": [
         {
            "field": "companyId",
            "message": "Must be digit"
         }
    ]
}

But when I try to implement the following unit test to this endpoint I get the org.springframework.web.client.HttpClientErrorException$UnprocessableEntity: 422 null

:

boardDTO.setCompanyId("");
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/json");
headers.set("Accept", "application/json");
HttpEntity<ValidationErrorResponses> httpEntity = new HttpEntity(boardDTO, headers);

ResponseEntity<ValidationErrorResponses> responseEntity = 
restTemplate.postForEntity(url, httpEntity, ValidationErrorResponses.class);

ValidationErrorResponses responseBody = responseEntity.getBody();
assertEquals(422, responseEntity.getStatusCode());
assertEquals("companyId", responseBody.getErrors().get(0).getField());
assertEquals("Must be digit", responseBody.getErrors().get(0).getMessage()); 

What should I do that my test work correctly?

To avoid testing implementation I suggest to test against the exception "has been thrown". The return message of the exception should be part of the unit-test from the exception:

@Test(expected = MethodArgumentNotValidException.class)
public void testMethodArgumentNotValidException() {
    boardDTO.setCompanyId("");
    HttpHeaders headers = new HttpHeaders();
    headers.set("Content-Type", "application/json");
    headers.set("Accept", "application/json");
    HttpEntity<ValidationErrorResponses> httpEntity = new HttpEntity(boardDTO, headers);

    ResponseEntity<ValidationErrorResponses> responseEntity = 
    restTemplate.postForEntity(url, httpEntity, ValidationErrorResponses.class);

}

Otherwise ask yourself if you can handle the result body with an user-acceptance test?

restTemplate.postForEntity will throw a HttpClientErrorException when the status is >= 400 and < 500. An easy way to write test here is:

try {
  restTemplate.postForEntity(url, httpEntity, ValidationErrorResponses.class);
  fail("should throw exception");
} catch (HttpClientErrorException e) {
  // Asserts go here using `e`
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM