Search in sources :

Example 21 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class ContextLoaderTests method testContextLoaderWithCustomContext.

@Test
public void testContextLoaderWithCustomContext() 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);
    WebApplicationContext wc = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    assertTrue("Correct WebApplicationContext exposed in ServletContext", wc instanceof SimpleWebApplicationContext);
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) Test(org.junit.Test)

Example 22 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class ContextLoaderTests method testContextLoaderWithInvalidLocation.

@Test
public void testContextLoaderWithInvalidLocation() throws Exception {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/WEB-INF/myContext.xml");
    ServletContextListener listener = new ContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    try {
        listener.contextInitialized(event);
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
        // expected
        assertTrue(ex.getCause() instanceof FileNotFoundException);
    }
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) FileNotFoundException(java.io.FileNotFoundException) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 23 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class ServletContextAwareProcessorTests method servletContextAwareWithServletContext.

@Test
public void servletContextAwareWithServletContext() {
    ServletContext servletContext = new MockServletContext();
    ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext);
    ServletContextAwareBean bean = new ServletContextAwareBean();
    assertNull(bean.getServletContext());
    processor.postProcessBeforeInitialization(bean, "testBean");
    assertNotNull("ServletContext should have been set", bean.getServletContext());
    assertEquals(servletContext, bean.getServletContext());
}
Also used : ServletContextAwareProcessor(org.springframework.web.context.support.ServletContextAwareProcessor) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 24 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class ServletContextAwareProcessorTests method servletContextAwareWithNonNullServletContextAndNullServletConfig.

@Test
public void servletContextAwareWithNonNullServletContextAndNullServletConfig() {
    ServletContext servletContext = new MockServletContext();
    ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext, null);
    ServletContextAwareBean bean = new ServletContextAwareBean();
    assertNull(bean.getServletContext());
    processor.postProcessBeforeInitialization(bean, "testBean");
    assertNotNull("ServletContext should have been set", bean.getServletContext());
    assertEquals(servletContext, bean.getServletContext());
}
Also used : ServletContextAwareProcessor(org.springframework.web.context.support.ServletContextAwareProcessor) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 25 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class ServletContextAwareProcessorTests method servletConfigAwareWithNonNullServletContextAndNullServletConfig.

@Test
public void servletConfigAwareWithNonNullServletContextAndNullServletConfig() {
    ServletContext servletContext = new MockServletContext();
    ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext, null);
    ServletConfigAwareBean bean = new ServletConfigAwareBean();
    assertNull(bean.getServletConfig());
    processor.postProcessBeforeInitialization(bean, "testBean");
    assertNull(bean.getServletConfig());
}
Also used : ServletContextAwareProcessor(org.springframework.web.context.support.ServletContextAwareProcessor) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Aggregations

MockServletContext (org.springframework.mock.web.test.MockServletContext)152 Test (org.junit.Test)120 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)56 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)50 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)41 ServletContext (javax.servlet.ServletContext)22 ServletContextEvent (javax.servlet.ServletContextEvent)21 HashMap (java.util.HashMap)18 Before (org.junit.Before)18 HttpServletResponse (javax.servlet.http.HttpServletResponse)16 MockFilterConfig (org.springframework.mock.web.test.MockFilterConfig)16 TestBean (org.springframework.tests.sample.beans.TestBean)15 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)14 MockServletConfig (org.springframework.mock.web.test.MockServletConfig)13 View (org.springframework.web.servlet.View)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 WebApplicationContext (org.springframework.web.context.WebApplicationContext)12 Resource (org.springframework.core.io.Resource)11 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)10 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)9