私の問題は、@ ExceptionHandlerで@ControllerAdviceも使用しているSpring @ RestControllerのテストに関連しています。コードは次のとおりです。
@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はMappingJackson2HttpMessageConverterをロードしていませんでした。
その行は私の問題を修正しました。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加