Package org.springframework.mock.web.test

Examples of org.springframework.mock.web.test.MockServletContext


  @Before
  public void setup() {
    this.serverContainer = mock(ServerContainer.class);

    this.servletContext = new MockServletContext();
    this.servletContext.setAttribute("javax.websocket.server.ServerContainer", this.serverContainer);

    this.webAppContext = new AnnotationConfigWebApplicationContext();
    this.webAppContext.register(Config.class);
    this.webAppContext.setServletContext(this.servletContext);
View Full Code Here


  private SpringConfigurator configurator;


  @Before
  public void setup() {
    this.servletContext = new MockServletContext();

    this.webAppContext = new AnnotationConfigWebApplicationContext();
    this.webAppContext.register(Config.class);

    this.contextLoader = new ContextLoader(this.webAppContext);
View Full Code Here

  private DispatcherServlet complexDispatcherServlet;

  @Override
  protected void setUp() throws ServletException {
    servletConfig = new MockServletConfig(new MockServletContext(), "simple");
    MockServletConfig complexConfig = new MockServletConfig(servletConfig.getServletContext(), "complex");
    complexConfig.addInitParameter("publishContext", "false");
    complexConfig.addInitParameter("class", "notWritable");
    complexConfig.addInitParameter("unknownParam", "someValue");
View Full Code Here

    complexDispatcherServlet.destroy();
    assertNull(myServlet.getServletConfig());
  }

  public void testWebApplicationContextLookup() {
    MockServletContext servletContext = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/invalid.do");

    try {
      RequestContextUtils.getWebApplicationContext(request);
      fail("Should have thrown IllegalStateException");
    }
    catch (IllegalStateException ex) {
      // expected
    }

    try {
      RequestContextUtils.getWebApplicationContext(request, servletContext);
      fail("Should have thrown IllegalStateException");
    }
    catch (IllegalStateException ex) {
      // expected
    }

    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
        new StaticWebApplicationContext());
    try {
      RequestContextUtils.getWebApplicationContext(request, servletContext);
    }
    catch (IllegalStateException ex) {
View Full Code Here

      fail("Should not have thrown IllegalStateException: " + ex.getMessage());
    }
  }

  public void testWithNoView() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/noview.do");
    MockHttpServletResponse response = new MockHttpServletResponse();

    complexDispatcherServlet.service(request, response);
    assertEquals("noview.jsp", response.getForwardedUrl());
View Full Code Here

    complexDispatcherServlet.service(request, response);
    assertEquals("noview.jsp", response.getForwardedUrl());
  }

  public void testWithNoViewNested() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/noview/simple.do");
    MockHttpServletResponse response = new MockHttpServletResponse();

    complexDispatcherServlet.service(request, response);
    assertEquals("noview/simple.jsp", response.getForwardedUrl());
View Full Code Here

  public void testWithNoViewAndSamePath() throws Exception {
    InternalResourceViewResolver vr = (InternalResourceViewResolver) complexDispatcherServlet
        .getWebApplicationContext().getBean("viewResolver2");
    vr.setSuffix("");

    MockServletContext servletContext = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/noview");
    MockHttpServletResponse response = new MockHttpServletResponse();

    try {
      complexDispatcherServlet.service(request, response);
View Full Code Here

      ex.printStackTrace();
    }
  }

  public void testDispatcherServletRefresh() throws ServletException {
    MockServletContext servletContext = new MockServletContext("org/springframework/web/context");
    DispatcherServlet servlet = new DispatcherServlet();

    servlet.init(new MockServletConfig(servletContext, "empty"));
    ServletContextAwareBean contextBean =
        (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean");
View Full Code Here

    servlet.destroy();
  }

  public void testDispatcherServletContextRefresh() throws ServletException {
    MockServletContext servletContext = new MockServletContext("org/springframework/web/context");
    DispatcherServlet servlet = new DispatcherServlet();

    servlet.init(new MockServletConfig(servletContext, "empty"));
    ServletContextAwareBean contextBean =
        (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean");
View Full Code Here

*/
public final class ContextLoaderTests {

  @Test
  public void testContextLoaderListenerWithDefaultContext() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
        "/org/springframework/web/context/WEB-INF/applicationContext.xml " +
        "/org/springframework/web/context/WEB-INF/context-addition.xml");
    ServletContextListener listener = new ContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);
    WebApplicationContext context = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext);
    assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext);
    LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
    assertTrue("Has father", context.containsBean("father"));
    assertTrue("Has rod", context.containsBean("rod"));
    assertTrue("Has kerry", context.containsBean("kerry"));
    assertTrue("Not destroyed", !lb.isDestroyed());
    assertFalse(context.containsBean("beans1.bean1"));
    assertFalse(context.containsBean("beans1.bean2"));
    listener.contextDestroyed(event);
    assertTrue("Destroyed", lb.isDestroyed());
    assertNull(sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
    assertNull(WebApplicationContextUtils.getWebApplicationContext(sc));
  }
View Full Code Here

TOP

Related Classes of org.springframework.mock.web.test.MockServletContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.