use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class ResponseEntityExceptionHandlerTests method controllerAdvice.
@Test
public void controllerAdvice() throws Exception {
StaticWebApplicationContext ctx = new StaticWebApplicationContext();
ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
ctx.refresh();
ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
resolver.setApplicationContext(ctx);
resolver.afterPropertiesSet();
ServletRequestBindingException ex = new ServletRequestBindingException("message");
assertThat(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex)).isNotNull();
assertThat(this.servletResponse.getStatus()).isEqualTo(400);
assertThat(this.servletResponse.getContentAsString()).isEqualTo("error content");
assertThat(this.servletResponse.getHeader("someHeader")).isEqualTo("someHeaderValue");
}
use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
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.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 org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class ResponseEntityExceptionHandlerTests method controllerAdviceWithNestedException.
@Test
public void controllerAdviceWithNestedException() {
StaticWebApplicationContext ctx = new StaticWebApplicationContext();
ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
ctx.refresh();
ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
resolver.setApplicationContext(ctx);
resolver.afterPropertiesSet();
IllegalStateException ex = new IllegalStateException(new ServletRequestBindingException("message"));
assertThat(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex)).isNull();
}
use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class RequestMappingHandlerAdapterTests method setupOnce.
@BeforeAll
public static void setupOnce() {
RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
adapter.setApplicationContext(new StaticWebApplicationContext());
adapter.afterPropertiesSet();
RESOLVER_COUNT = adapter.getArgumentResolvers().size();
INIT_BINDER_RESOLVER_COUNT = adapter.getInitBinderArgumentResolvers().size();
HANDLER_COUNT = adapter.getReturnValueHandlers().size();
}
use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.
the class RequestMappingHandlerAdapterTests method modelAttributeAdviceInParentContext.
@Test
public void modelAttributeAdviceInParentContext() throws Exception {
StaticWebApplicationContext parent = new StaticWebApplicationContext();
parent.registerSingleton("maa", ModelAttributeAdvice.class);
parent.refresh();
this.webAppContext.setParent(parent);
this.webAppContext.refresh();
HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
this.handlerAdapter.afterPropertiesSet();
ModelAndView mav = this.handlerAdapter.handle(this.request, this.response, handlerMethod);
assertThat(mav.getModel().get("attr1")).isEqualTo("lAttr1");
assertThat(mav.getModel().get("attr2")).isEqualTo("gAttr2");
}
Aggregations