use of cn.taketoday.web.handler.method.ExceptionHandlerAnnotationExceptionHandler in project today-framework by TAKETODAY.
the class ResponseEntityExceptionHandlerTests method controllerAdvice.
@Test
public void controllerAdvice() throws Exception {
StaticWebApplicationContext ctx = new StaticWebApplicationContext();
ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
ctx.registerSingleton("parameterResolvingRegistry", ParameterResolvingRegistry.class);
ctx.registerSingleton("returnValueHandlerManager", ReturnValueHandlerManager.class);
ctx.refresh();
ExceptionHandlerAnnotationExceptionHandler resolver = new ExceptionHandlerAnnotationExceptionHandler();
resolver.setApplicationContext(ctx);
resolver.afterPropertiesSet();
RequestBindingException ex = new RequestBindingException("message");
assertThat(resolver.handleException(request, ex, null)).isNotNull();
assertThat(this.servletResponse.getStatus()).isEqualTo(400);
assertThat(this.servletResponse.getContentAsString()).isEqualTo("error content");
assertThat(this.servletResponse.getHeader("someHeader")).isEqualTo("someHeaderValue");
}
use of cn.taketoday.web.handler.method.ExceptionHandlerAnnotationExceptionHandler in project today-framework by TAKETODAY.
the class ResponseEntityExceptionHandlerTests method controllerAdviceWithNestedException.
@Test
public void controllerAdviceWithNestedException() throws Exception {
StaticWebApplicationContext ctx = new StaticWebApplicationContext();
ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
ctx.registerSingleton("parameterResolvingRegistry", ParameterResolvingRegistry.class);
ctx.registerSingleton("returnValueHandlerManager", ReturnValueHandlerManager.class);
ctx.refresh();
ExceptionHandlerAnnotationExceptionHandler resolver = new ExceptionHandlerAnnotationExceptionHandler();
resolver.setApplicationContext(ctx);
resolver.afterPropertiesSet();
IllegalStateException ex = new IllegalStateException(new RequestBindingException("message"));
assertThat(resolver.handleException(new ServletRequestContext(null, this.servletRequest, this.servletResponse), ex, null)).isNull();
}
Aggregations