Search in sources :

Example 1 with ExceptionHandlerAnnotationExceptionHandler

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");
}
Also used : RequestBindingException(cn.taketoday.web.bind.RequestBindingException) StaticWebApplicationContext(cn.taketoday.web.context.support.StaticWebApplicationContext) ExceptionHandlerAnnotationExceptionHandler(cn.taketoday.web.handler.method.ExceptionHandlerAnnotationExceptionHandler) Test(org.junit.jupiter.api.Test)

Example 2 with ExceptionHandlerAnnotationExceptionHandler

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();
}
Also used : RequestBindingException(cn.taketoday.web.bind.RequestBindingException) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) StaticWebApplicationContext(cn.taketoday.web.context.support.StaticWebApplicationContext) ExceptionHandlerAnnotationExceptionHandler(cn.taketoday.web.handler.method.ExceptionHandlerAnnotationExceptionHandler) Test(org.junit.jupiter.api.Test)

Aggregations

RequestBindingException (cn.taketoday.web.bind.RequestBindingException)2 StaticWebApplicationContext (cn.taketoday.web.context.support.StaticWebApplicationContext)2 ExceptionHandlerAnnotationExceptionHandler (cn.taketoday.web.handler.method.ExceptionHandlerAnnotationExceptionHandler)2 Test (org.junit.jupiter.api.Test)2 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)1