Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty Exception Body in Spring MVC Test

I am having trouble while trying to make MockMvc to include the exception message in the response body. I have a controller as follows:

@RequestMapping("/user/new")
public AbstractResponse create(@Valid NewUserParameters params, BindingResult bindingResult) {
    if (bindingResult.hasErrors()) throw BadRequestException.of(bindingResult);
    // ...
}

where BadRequestException looks sth like this:

@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "bad request")
public class BadRequestException extends IllegalArgumentException {

    public BadRequestException(String cause) { super(cause); }

    public static BadRequestException of(BindingResult bindingResult) { /* ... */ }

}

And I run the following test against /user/new controller:

@Test
public void testUserNew() throws Exception {
    getMockMvc().perform(post("/user/new")
            .param("username", username)
            .param("password", password))
            .andDo(print())
            .andExpect(status().isOk());
}

which prints the following output:

  Resolved Exception:
                Type = controller.exception.BadRequestException

        ModelAndView:
           View name = null
                View = null
               Model = null

            FlashMap:

MockHttpServletResponse:
              Status = 400
       Error message = bad request
             Headers = {X-Content-Type-Options=[nosniff], X-XSS-Protection=[1; mode=block], Cache-Control=[no-cache, no-store, max-age=0, must-revalidate], Pragma=[no-cache], Expires=[0], X-Frame-Options=[DENY]}
        Content type = null
                Body = 
       Forwarded URL = null
      Redirected URL = null
             Cookies = []

Does anybody have an idea on why is Body missing in the print() output?

Edit: I am not using any custom exception handlers and the code works as expected when I run the server. That is, running the application and making the same request to the server returns back

{"timestamp":1423076185822,
 "status":400,
 "error":"Bad Request",
 "exception":"controller.exception.BadRequestException",
 "message":"binding failed for field(s): password, username, username",
 "path":"/user/new"}

as expected. Hence, there is a problem with the MockMvc I suppose. It somehow misses to capture the message field of the exception, whereas the default exception handler of the regular application server works as expected.

like image 275
Volkan Yazıcı Avatar asked Feb 04 '15 15:02

Volkan Yazıcı


People also ask

Does rest assured work with @springboottest with the same controller?

As you can see the request is sent correctly but the response values are all null. When i test the same controller using @SpringBootTest with Rest Assured it works fine.

How do I test that my controller is throwing the correct exceptions?

Finally, we're going to see how to test that our controller is throwing the correct exceptions. The first step is to create a test class and create an instance of MockMvc: Next, let's create the test cases for each of the values that our service can receive:

What happens when we throw an exception from a restcontroller?

In conclusion, when we throw an exception from a RestController, the service response is automatically mapped to a 500 response code, and the stack trace of the exception is included in the response body. 3. Mapping Exceptions to HTTP Response Codes Now we're going to learn how to map our exceptions to different response codes other than 500.


2 Answers

After opening a ticket for the issue, I was told that the error message in the body is taken care of by Spring Boot which configures error mappings at the Servlet container level and since Spring MVC Test runs with a mock Servlet request/response, there is no such error mapping. Further, they recommended me to create at least one @WebIntegrationTest and stick to Spring MVC Test for my controller logic.

Eventually, I decided to go with my own custom exception handler and stick to MockMvc for the rest as before.

@ControllerAdvice
public class CustomExceptionHandler {

    @ExceptionHandler(Throwable.class)
    public @ResponseBody
    ExceptionResponse handle(HttpServletResponse response, Throwable throwable) {
        HttpStatus status = Optional
                .ofNullable(AnnotationUtils.getAnnotation(throwable.getClass(), ResponseStatus.class))
                .map(ResponseStatus::value)
                .orElse(HttpStatus.INTERNAL_SERVER_ERROR);
        response.setStatus(status.value());
        return new ExceptionResponse(throwable.getMessage());
    }

}

@Data
public class ExceptionResponse extends AbstractResponse {

    private final long timestamp = System.currentTimeMillis();

    private final String message;

    @JsonCreator
    public ExceptionResponse(String message) {
        checkNotNull(message, "message == NULL");
        this.message = message;
    }

}
like image 60
Volkan Yazıcı Avatar answered Oct 23 '22 03:10

Volkan Yazıcı


This likely means that you either didn't handle the exception or you've really left the body empty. To handle the exception either add an error handler in the controller

@ExceptionHandler
public @ResponseBody String handle(BadRequestException e) {
    return "I'm the body";
}

or user the global error handler if you're on 3.2 or above

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public @ResponseBody String handleBadRequestException(BadRequestException ex) {
        return "I'm the body";
    }
}

with this the body will be populate, you should populate it with your error message

like image 33
Master Slave Avatar answered Oct 23 '22 03:10

Master Slave