[英]Testing thrown exception in controller
I want to perform a test on a controller method which throws an exception.我想对引发异常的控制器方法执行测试。 The method is something like this:该方法是这样的:
@RequestMapping("/do")
public ResponseEntity doIt(@RequestBody Request request) throws Exception {
throw new NullPointerException();
}
When I try to test this method with following code part,当我尝试使用以下代码部分测试此方法时,
mockMvc.perform(post("/do")
.contentType(MediaType.APPLICATION_JSON)
.content(JSON.toJson(request)))
NestedServletException
is thrown from Spring libraries. NestedServletException
是从 Spring 库中抛出的。 How can I test that NullPointerException
is thrown instead of NestedServletException
?如何测试抛出NullPointerException
而不是NestedServletException
?
Easier way is to inject @ExceptionHandler
into your Spring Test Context or it throws exception right in MockMvc.perform()
just before .andExpect()
.更简单的方法是将@ExceptionHandler
注入你的 Spring 测试上下文,或者它在MockMvc.perform()
之前的.andExpect()
抛出异常。
@ContextConfiguration(classes = { My_ExceptionHandler_AreHere.class })
@AutoConfigureMockMvc
public class Test {
@Autowired
private MockMvc mvc;
@Test
public void test() {
RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/update")
.param("branchId", "13000")
.param("triggerId", "1");
MvcResult mvcResult = mvc.perform(requestBuilder)
.andExpect(MockMvcResultMatchers.status().is4xxClientError())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(__ -> Assert.assertThat(
__.getResolvedException(),
CoreMatchers.instanceOf(SecurityException.class)))
.andReturn();
}
That way MvcResult.getResolvedException()
holds @Controller
's exception!这样MvcResult.getResolvedException()
持有@Controller
的异常!
https://stackoverflow.com/a/61016827/173149 https://stackoverflow.com/a/61016827/173149
Our solution is rather a workaround: The exception is caught in advice and error body is returned as HTTP response.我们的解决方案是一种解决方法:异常在通知中捕获,错误正文作为 HTTP 响应返回。 Here is how the mock works:这是模拟的工作原理:
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller)
.setHandlerExceptionResolvers(withExceptionControllerAdvice())
.build();
private ExceptionHandlerExceptionResolver withExceptionControllerAdvice() {
final ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() {
@Override
protected ServletInvocableHandlerMethod getExceptionHandlerMethod(final HandlerMethod handlerMethod, final Exception exception) {
Method method = new ExceptionHandlerMethodResolver(TestAdvice.class).resolveMethod(exception);
if (method != null) {
return new ServletInvocableHandlerMethod(new TestAdvice(), method);
}
return super.getExceptionHandlerMethod(handlerMethod, exception);
}
};
exceptionResolver.afterPropertiesSet();
return exceptionResolver;
}
Advice class:咨询类:
@ControllerAdvice
public class TestAdvice {
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Object exceptionHandler(Exception e) {
return new HttpEntity<>(e.getMessage());
}
}
After than, following test method passes successfully:之后,以下测试方法成功通过:
@Test
public void testException
mockMvc.perform(post("/exception/path"))
.andExpect(status().is5xxServerError())
.andExpect(content().string("Exception body"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.