简体   繁体   English

在控制器中测试抛出的异常

[英]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.

相关问题 测试空白空间时引发异常 - exception thrown when testing for blank space 抛出预期异常后测试响应 - Testing response after expected exception is thrown 如果在控制器中引发异常,则Spring JPA回滚事务 - Spring JPA rollback transaction if Exception thrown in controller 如何在从服务引发异常的 rest controller 中模拟异常 - How to mock a exception in rest controller where the exception is thrown from a service 使用mockito进行单元测试时抛出WrongTypeOfReturnValue异常 - WrongTypeOfReturnValue exception thrown when unit testing using mockito 406在Spring控制器中抛出异常时接受带有text / csv的接受头 - 406 when exception thrown in Spring controller with accept header of text/csv 如何发送messages.properties键到在控制器中引发的异常? - How to send a messages.properties key to an exception being thrown in a controller? 如何从Hibernate DAO引发异常到Spring MVC控制器? - How is exception thrown from Hibernate DAO to Spring MVC controller? 如何在ControllerAdvice中使用ExceptionHandler处理控制器中ExceptionHandler抛出的异常? - How to handle exception thrown from ExceptionHandler in controller with ExceptionHandler in ControllerAdvice? Spring RestController:Rest Controller 中抛出异常时的 HTTP 406 - Spring RestController: HTTP 406 when an exception is thrown in Rest Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM