java – 使用@ControllerAdvice测试@RestController

前端之家收集整理的这篇文章主要介绍了java – 使用@ControllerAdvice测试@RestController前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我的问题与测试Spring @RestController有关,它也使用@ControllerAdvice和@ExceptionHandler.这是代码

@ControllerAdvice类:

  1. @ControllerAdvice
  2. public class MyAppExceptionHandler {
  3. @ExceptionHandler({ NoSuchEntityException.class })
  4. @ResponseStatus(value = HttpStatus.NOT_FOUND)
  5. public @ResponseBody
  6. ErrorDTO handleNotFoundException(Exception ex) throws IOException {
  7. return new ErrorDTO.Builder().setStatus(HttpStatus.NOT_FOUND)
  8. .setCause(ex.getClass().getName())
  9. .setThrowable(ex).build();
  10. }
  11. }

在应用程序中使用它时一切正常 – 通过JSON解释完美地获得404响应,但在测试期间尝试使用它时 – 会发生不好的事情.

我的考试班:

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(classes = { WebConfig.class })
  3. @WebAppConfiguration
  4. public class SomeTest {
  5. @Mock
  6. private SomeService service;
  7. @InjectMocks
  8. private SomeController controller;
  9. private MockMvc mvc;
  10. private ExceptionHandlerExceptionResolver createExceptionResolver() {
  11. ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() {
  12. @Override
  13. protected ServletInvocableHandlerMethod getExceptionHandlerMethod(
  14. HandlerMethod handlerMethod,Exception exception) {
  15. Method method = new ExceptionHandlerMethodResolver(
  16. MyAppExceptionHandler.class).resolveMethod(exception);
  17. return new ServletInvocableHandlerMethod(
  18. new MyAppExceptionHandler(),method);
  19. }
  20. };
  21. exceptionResolver.afterPropertiesSet();
  22. return exceptionResolver;
  23. }
  24. @Before
  25. public void setup() {
  26. MockitoAnnotations.initMocks(this);
  27. mvc = MockMvcBuilders.standaloneSetup(controller)
  28. .setHandlerExceptionResolvers(createExceptionResolver())
  29. .build();
  30. }
  31. @Test
  32. public void thatExceptionHappens() throws Exception {
  33. when(service.get(10)).thenThrow(new NoSuchEntityException(Some.class,10));
  34. mvc.perform(get("/api/some/10")).andExpect(status().isNotFound());
  35. }
  36. }

尝试运行时:

  1. 2014-07-15 19:35:01.376 [main] ERROR com.package.SomeTest$1 - Failed to invoke @ExceptionHandler method: public com.package.ErrorDTO com.package.MyAppExceptionHandler.handleNotFoundException(java.lang.Exception) throws java.io.IOException
  2. org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

我认为可能在测试我的@ExceptionHandler期间没有加载MappingJackson2HttpMessageConverter(但它在WebConfig.class中配置,并且在尝试执行典型测试时 – 一个没有抛出任何异常 – 一切正常).

在此先感谢您的帮助.

最佳答案
我不确定这是否是最佳解决方案(我想听听另一个),
但这就是我如何解决你面临的问题:

将此行添加到createExceptionResolver():

  1. exceptionResolver.getMessageConverters().add(
  2. new MappingJackson2HttpMessageConverter());

像这样的东西:

  1. private ExceptionHandlerExceptionResolver createExceptionResolver() {
  2. ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() {
  3. @Override
  4. protected ServletInvocableHandlerMethod getExceptionHandlerMethod(
  5. HandlerMethod handlerMethod,Exception exception) {
  6. Method method = new ExceptionHandlerMethodResolver(
  7. MyAppExceptionHandler.class).resolveMethod(exception);
  8. return new ServletInvocableHandlerMethod(
  9. new MyAppExceptionHandler(),method);
  10. }
  11. };
  12. exceptionResolver.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
  13. exceptionResolver.afterPropertiesSet();
  14. return exceptionResolver;
  15. }

由于某些我不知道的原因,Spring没有加载我的MappingJackson2HttpMessageConverter.
这条线修复了我的问题.

猜你在找的Spring相关文章