12

我在尝试让 MockMvc 在响应正文中包含异常消息时遇到问题。我有一个控制器如下:

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

BadRequestException看起来像这样的地方:

@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) { /* ... */ }

}

我对/user/new控制器运行以下测试:

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

打印以下输出:

  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 = []

有没有人知道为什么输出中Body缺少print()

编辑:我没有使用任何自定义异常处理程序,并且在我运行服务器时代码按预期工作。也就是说,运行应用程序并向服务器发出相同的请求会返回

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

正如预期的那样。因此,MockMvc我认为存在问题。它以某种方式错过了捕获message异常的字段,而常规应用程序服务器的默认异常处理程序按预期工作。

4

3 回答 3

10

在为该问题打开票证后,我被告知正文中的错误消息由 Spring Boot 处理,它在 Servlet 容器级别配置错误映射,并且由于 Spring MVC 测试以模拟 Servlet 请求/响应运行,因此有没有这样的错误映射。此外,他们建议我为我的控制器逻辑创建至少一个@WebIntegrationTest并坚持使用 Spring MVC 测试。

最终,我决定使用我自己的自定义异常处理程序,并MockMvc像以前一样坚持其余的工作。

@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;
    }

}
于 2015-02-10T09:34:39.463 回答
3

这可能意味着您要么没有处理异常,要么真的让正文为空。要处理异常,请在控制器中添加错误处理程序

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

如果您使用的是 3.2 或更高版本,则使用全局错误处理程序

@ControllerAdvice
public class GlobalExceptionHandler {

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

有了这个主体将被填充,你应该用你的错误消息填充它

于 2015-02-04T16:07:18.943 回答
0

更新的解决方案:

如果您不想进行完整的集成测试,但仍想确保消息符合预期,您仍然可以执行以下操作:

String errorMessage = getMockMvc()
                        .perform(post("/user/new"))
                        ...
                        .andReturn().getResolvedException().getMessage();

assertThat(errorMessage, is("This is the error message!");
于 2020-09-15T21:19:10.980 回答