我的问题与测试 Spring @RestController 相关,它也使用 @ControllerAdvice 和 @ExceptionHandler 。这是代码:
@ControllerAdvice类:
@ControllerAdvice
public class MyAppExceptionHandler {
@ExceptionHandler({ NoSuchEntityException.class })
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public @ResponseBody
ErrorDTO handleNotFoundException(Exception ex) throws IOException {
return new ErrorDTO.Builder().setStatus(HttpStatus.NOT_FOUND)
.setCause(ex.getClass().getName())
.setThrowable(ex).build();
}
}
当在应用程序中使用它时,一切正常 - 完美地获得带有 JSON 解释的 404 响应,但是当尝试在测试期间使用它时 - 糟糕的事情发生了。
我的测试课:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { WebConfig.class })
@WebAppConfiguration
public class SomeTest {
@Mock
private SomeService service;
@InjectMocks
private SomeController controller;
private MockMvc mvc;
private ExceptionHandlerExceptionResolver createExceptionResolver() {
ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() {
@Override
protected ServletInvocableHandlerMethod getExceptionHandlerMethod(
HandlerMethod handlerMethod, Exception exception) {
Method method = new ExceptionHandlerMethodResolver(
MyAppExceptionHandler.class).resolveMethod(exception);
return new ServletInvocableHandlerMethod(
new MyAppExceptionHandler(), method);
}
};
exceptionResolver.afterPropertiesSet();
return exceptionResolver;
}
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mvc = MockMvcBuilders.standaloneSetup(controller)
.setHandlerExceptionResolvers(createExceptionResolver())
.build();
}
@Test
public void thatExceptionHappens() throws Exception {
when(service.get(10)).thenThrow(new NoSuchEntityException(Some.class, 10));
mvc.perform(get("/api/some/10")).andExpect(status().isNotFound());
}
}
当尝试运行它时:
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
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
我认为在测试我的 @ExceptionHandler 期间可能没有加载 MappingJackson2HttpMessageConverter (但是它是在 WebConfig.class 中配置的,并且在尝试执行典型测试时 - 不抛出任何异常 - 一切正常)。
在此先感谢您的帮助。
我不确定这是否是最好的解决方案(我想听听另一位的解决方案),
但这就是我解决您面临的问题的方法:
将此行添加到您的createExceptionResolver()
:
exceptionResolver.getMessageConverters().add(
new MappingJackson2HttpMessageConverter());
像这样的事情:
private ExceptionHandlerExceptionResolver createExceptionResolver() {
ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() {
@Override
protected ServletInvocableHandlerMethod getExceptionHandlerMethod(
HandlerMethod handlerMethod, Exception exception) {
Method method = new ExceptionHandlerMethodResolver(
MyAppExceptionHandler.class).resolveMethod(exception);
return new ServletInvocableHandlerMethod(
new MyAppExceptionHandler(), method);
}
};
exceptionResolver.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
exceptionResolver.afterPropertiesSet();
return exceptionResolver;
}
由于某种我不知道的原因,Spring 没有加载我的映射Jackson2HttpMessageConverter.
那条线解决了我的问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)