Search in sources :

Example 71 with StaticWebApplicationContext

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");
}
Also used : ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 72 with StaticWebApplicationContext

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");
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 73 with StaticWebApplicationContext

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();
}
Also used : ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 74 with StaticWebApplicationContext

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();
}
Also used : StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 75 with StaticWebApplicationContext

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");
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Aggregations

StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)101 Test (org.junit.jupiter.api.Test)54 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)29 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)21 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)20 MockServletContext (org.springframework.mock.web.test.MockServletContext)16 Test (org.junit.Test)15 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14 ServletContext (jakarta.servlet.ServletContext)11 View (org.springframework.web.servlet.View)11 BeforeEach (org.junit.jupiter.api.BeforeEach)10 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)10 MockFilterConfig (org.springframework.web.testfixture.servlet.MockFilterConfig)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 Before (org.junit.Before)8 WebApplicationContext (org.springframework.web.context.WebApplicationContext)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)6 Properties (java.util.Properties)5