Search in sources :

Example 51 with MockServletContext

use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.

the class HttpRequestHandlerTests method testHttpRequestHandlerServletPassThrough.

@Test
public void testHttpRequestHandlerServletPassThrough() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.getBeanFactory().registerSingleton("myHandler", (HttpRequestHandler) (req, res) -> {
        assertThat(req).isSameAs(request);
        assertThat(res).isSameAs(response);
        String exception = request.getParameter("exception");
        if ("ServletException".equals(exception)) {
            throw new ServletException("test");
        }
        if ("IOException".equals(exception)) {
            throw new IOException("test");
        }
        res.getWriter().write("myResponse");
    });
    wac.setServletContext(servletContext);
    wac.refresh();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    Servlet servlet = new HttpRequestHandlerServlet();
    servlet.init(new MockServletConfig(servletContext, "myHandler"));
    servlet.service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myResponse");
    request.setParameter("exception", "ServletException");
    assertThatExceptionOfType(ServletException.class).isThrownBy(() -> servlet.service(request, response)).withMessage("test");
    request.setParameter("exception", "IOException");
    assertThatIOException().isThrownBy(() -> servlet.service(request, response)).withMessage("test");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WebApplicationContext(org.springframework.web.context.WebApplicationContext) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) Test(org.junit.jupiter.api.Test) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Servlet(jakarta.servlet.Servlet) HttpRequestHandler(org.springframework.web.HttpRequestHandler) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletException(jakarta.servlet.ServletException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Servlet(jakarta.servlet.Servlet) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) IOException(java.io.IOException) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 52 with MockServletContext

use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.

the class ContextLoaderTests method contextLoaderListenerWithProgrammaticAndGlobalInitializers.

@Test
void contextLoaderListenerWithProgrammaticAndGlobalInitializers() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
    sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, TestWebContextInitializer.class.getName());
    ContextLoaderListener listener = new ContextLoaderListener();
    listener.setContextInitializers(new TestContextInitializer());
    listener.contextInitialized(new ServletContextEvent(sc));
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
    TestBean testBean = wac.getBean(TestBean.class);
    assertThat(testBean.getName()).isEqualTo("testName");
    assertThat(wac.getServletContext().getAttribute("initialized")).isNotNull();
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletContextEvent(jakarta.servlet.ServletContextEvent) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 53 with MockServletContext

use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.

the class ContextLoaderTests method contextLoaderListenerWithProgrammaticInitializers.

@Test
void contextLoaderListenerWithProgrammaticInitializers() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
    ContextLoaderListener listener = new ContextLoaderListener();
    listener.setContextInitializers(new TestContextInitializer(), new TestWebContextInitializer());
    listener.contextInitialized(new ServletContextEvent(sc));
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
    TestBean testBean = wac.getBean(TestBean.class);
    assertThat(testBean.getName()).isEqualTo("testName");
    assertThat(wac.getServletContext().getAttribute("initialized")).isNotNull();
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletContextEvent(jakarta.servlet.ServletContextEvent) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 54 with MockServletContext

use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.

the class ContextLoaderTests method contextLoaderWithCustomContext.

@Test
void contextLoaderWithCustomContext() throws Exception {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM, "org.springframework.web.servlet.SimpleWebApplicationContext");
    ServletContextListener listener = new ContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);
    String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
    WebApplicationContext wc = (WebApplicationContext) sc.getAttribute(contextAttr);
    boolean condition = wc instanceof SimpleWebApplicationContext;
    assertThat(condition).as("Correct WebApplicationContext exposed in ServletContext").isTrue();
}
Also used : ServletContextListener(jakarta.servlet.ServletContextListener) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletContextEvent(jakarta.servlet.ServletContextEvent) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 55 with MockServletContext

use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.

the class ContextLoaderTests method contextLoaderWithInvalidContext.

@Test
void contextLoaderWithInvalidContext() throws Exception {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM, "org.springframework.web.context.support.InvalidWebApplicationContext");
    ServletContextListener listener = new ContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> listener.contextInitialized(event)).withCauseInstanceOf(ClassNotFoundException.class);
}
Also used : ServletContextListener(jakarta.servlet.ServletContextListener) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletContextEvent(jakarta.servlet.ServletContextEvent) Test(org.junit.jupiter.api.Test)

Aggregations

MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)138 Test (org.junit.jupiter.api.Test)112 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)44 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)38 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)24 ServletContext (jakarta.servlet.ServletContext)22 ServletContextEvent (jakarta.servlet.ServletContextEvent)21 BeforeEach (org.junit.jupiter.api.BeforeEach)17 WebApplicationContext (org.springframework.web.context.WebApplicationContext)17 MockFilterConfig (org.springframework.web.testfixture.servlet.MockFilterConfig)14 MockServletConfig (org.springframework.web.testfixture.servlet.MockServletConfig)13 Resource (org.springframework.core.io.Resource)12 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)12 HashMap (java.util.HashMap)11 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)11 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)10 TestBean (org.springframework.beans.testfixture.beans.TestBean)9 ClassPathResource (org.springframework.core.io.ClassPathResource)9 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)9 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)7