Search in sources :

Example 1 with MockServletConfig

use of cn.taketoday.web.testfixture.servlet.MockServletConfig in project today-framework by TAKETODAY.

the class ResponseEntityExceptionHandlerTests method controllerAdviceWithinDispatcherServlet.

@Test
public void controllerAdviceWithinDispatcherServlet() throws Exception {
    StaticWebApplicationContext ctx = new StaticWebApplicationContext();
    ctx.registerSingleton("controller", ExceptionThrowingController.class);
    ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
    ctx.registerSingleton("parameterResolvingRegistry", ParameterResolvingRegistry.class);
    ctx.registerSingleton("returnValueHandlerManager", ReturnValueHandlerManager.class);
    ctx.refresh();
    DispatcherServlet servlet = new DispatcherServlet(ctx);
    servlet.init(new MockServletConfig());
    servlet.service(this.servletRequest, this.servletResponse);
    assertThat(this.servletResponse.getStatus()).isEqualTo(400);
    assertThat(this.servletResponse.getContentAsString()).isEqualTo("error content");
    assertThat(this.servletResponse.getHeader("someHeader")).isEqualTo("someHeaderValue");
}
Also used : DispatcherServlet(cn.taketoday.web.servlet.DispatcherServlet) MockServletConfig(cn.taketoday.web.testfixture.servlet.MockServletConfig) StaticWebApplicationContext(cn.taketoday.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 2 with MockServletConfig

use of cn.taketoday.web.testfixture.servlet.MockServletConfig in project today-framework by TAKETODAY.

the class ResponseEntityExceptionHandlerTests method controllerAdviceWithNestedExceptionWithinDispatcherServlet.

@Test
public void controllerAdviceWithNestedExceptionWithinDispatcherServlet() throws Exception {
    StaticWebApplicationContext ctx = new StaticWebApplicationContext();
    ctx.registerSingleton("controller", NestedExceptionThrowingController.class);
    ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
    ctx.registerSingleton("parameterResolvingRegistry", ParameterResolvingRegistry.class);
    ctx.registerSingleton("returnValueHandlerManager", ReturnValueHandlerManager.class);
    ctx.refresh();
    DispatcherServlet servlet = new DispatcherServlet(ctx);
    servlet.init(new MockServletConfig());
    try {
        servlet.service(this.servletRequest, this.servletResponse);
    } catch (ServletException ex) {
        boolean condition1 = ex.getCause() instanceof IllegalStateException;
        assertThat(condition1).isTrue();
        boolean condition = ex.getCause().getCause() instanceof RequestBindingException;
        assertThat(condition).isTrue();
    }
}
Also used : ServletException(jakarta.servlet.ServletException) RequestBindingException(cn.taketoday.web.bind.RequestBindingException) DispatcherServlet(cn.taketoday.web.servlet.DispatcherServlet) MockServletConfig(cn.taketoday.web.testfixture.servlet.MockServletConfig) StaticWebApplicationContext(cn.taketoday.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Aggregations

StaticWebApplicationContext (cn.taketoday.web.context.support.StaticWebApplicationContext)2 DispatcherServlet (cn.taketoday.web.servlet.DispatcherServlet)2 MockServletConfig (cn.taketoday.web.testfixture.servlet.MockServletConfig)2 Test (org.junit.jupiter.api.Test)2 RequestBindingException (cn.taketoday.web.bind.RequestBindingException)1 ServletException (jakarta.servlet.ServletException)1