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");
}
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();
}
}
Aggregations