Search in sources :

Example 1 with MockServletConfig

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

the class ContextLoaderTests method testFrameworkServletWithCustomLocation.

@Test
public void testFrameworkServletWithCustomLocation() throws Exception {
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.setContextConfigLocation("/org/springframework/web/context/WEB-INF/testNamespace.xml " + "/org/springframework/web/context/WEB-INF/context-addition.xml");
    servlet.init(new MockServletConfig(new MockServletContext(""), "test"));
    assertTrue(servlet.getWebApplicationContext().containsBean("kerry"));
    assertTrue(servlet.getWebApplicationContext().containsBean("kerryX"));
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 2 with MockServletConfig

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

the class AbstractServletHandlerMethodTests method initServlet.

/**
	 * Initialize a DispatcherServlet instance registering zero or more controller classes
	 * and also providing additional bean definitions through a callback.
	 */
@SuppressWarnings("serial")
protected WebApplicationContext initServlet(final ApplicationContextInitializer<GenericWebApplicationContext> initializer, final Class<?>... controllerClasses) throws ServletException {
    final GenericWebApplicationContext wac = new GenericWebApplicationContext();
    servlet = new DispatcherServlet() {

        @Override
        protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
            for (Class<?> clazz : controllerClasses) {
                wac.registerBeanDefinition(clazz.getSimpleName(), new RootBeanDefinition(clazz));
            }
            Class<?> mappingType = RequestMappingHandlerMapping.class;
            RootBeanDefinition beanDef = new RootBeanDefinition(mappingType);
            beanDef.getPropertyValues().add("removeSemicolonContent", "false");
            wac.registerBeanDefinition("handlerMapping", beanDef);
            Class<?> adapterType = RequestMappingHandlerAdapter.class;
            wac.registerBeanDefinition("handlerAdapter", new RootBeanDefinition(adapterType));
            Class<?> resolverType = ExceptionHandlerExceptionResolver.class;
            wac.registerBeanDefinition("requestMappingResolver", new RootBeanDefinition(resolverType));
            resolverType = ResponseStatusExceptionResolver.class;
            wac.registerBeanDefinition("responseStatusResolver", new RootBeanDefinition(resolverType));
            resolverType = DefaultHandlerExceptionResolver.class;
            wac.registerBeanDefinition("defaultResolver", new RootBeanDefinition(resolverType));
            if (initializer != null) {
                initializer.initialize(wac);
            }
            wac.refresh();
            return wac;
        }
    };
    servlet.init(new MockServletConfig());
    return wac;
}
Also used : ResponseStatusExceptionResolver(org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 3 with MockServletConfig

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

the class ServletAnnotationControllerHandlerMethodTests method nullCommandController.

@Test
public void nullCommandController() throws Exception {
    initServletWithControllers(MyNullCommandController.class);
    getServlet().init(new MockServletConfig());
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath");
    request.setUserPrincipal(new OtherPrincipal());
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myView", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 4 with MockServletConfig

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

the class ServletAnnotationControllerHandlerMethodTests method parameterDispatchingController.

@Test
public void parameterDispatchingController() throws Exception {
    final MockServletContext servletContext = new MockServletContext();
    final MockServletConfig servletConfig = new MockServletConfig(servletContext);
    WebApplicationContext webAppContext = initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext wac) {
            wac.setServletContext(servletContext);
            AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
            wac.getBeanFactory().registerResolvableDependency(ServletConfig.class, servletConfig);
        }
    }, MyParameterDispatchingController.class);
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpSession session = request.getSession();
    getServlet().service(request, response);
    assertEquals("myView", response.getContentAsString());
    assertSame(servletContext, request.getAttribute("servletContext"));
    assertSame(servletConfig, request.getAttribute("servletConfig"));
    assertSame(session.getId(), request.getAttribute("sessionId"));
    assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
    assertSame(request.getLocale(), request.getAttribute("locale"));
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    response = new MockHttpServletResponse();
    session = request.getSession();
    getServlet().service(request, response);
    assertEquals("myView", response.getContentAsString());
    assertSame(servletContext, request.getAttribute("servletContext"));
    assertSame(servletConfig, request.getAttribute("servletConfig"));
    assertSame(session.getId(), request.getAttribute("sessionId"));
    assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("view", "other");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myOtherView", response.getContentAsString());
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("view", "my");
    request.addParameter("lang", "de");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myLangView", response.getContentAsString());
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("surprise", "!");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("mySurpriseView", response.getContentAsString());
    MyParameterDispatchingController deserialized = (MyParameterDispatchingController) SerializationTestUtils.serializeAndDeserialize(webAppContext.getBean(MyParameterDispatchingController.class.getSimpleName()));
    assertNotNull(deserialized.request);
    assertNotNull(deserialized.session);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 5 with MockServletConfig

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

the class ServletAnnotationControllerHandlerMethodTests method relativePathDispatchingController.

@Test
public void relativePathDispatchingController() throws Exception {
    initServletWithControllers(MyRelativePathDispatchingController.class);
    getServlet().init(new MockServletConfig());
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myApp/myHandle");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myView", response.getContentAsString());
    request = new MockHttpServletRequest("GET", "/myApp/myOther");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myOtherView", response.getContentAsString());
    request = new MockHttpServletRequest("GET", "/myApp/myLang");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myLangView", response.getContentAsString());
    request = new MockHttpServletRequest("GET", "/myApp/surprise.do");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("mySurpriseView", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockServletConfig (org.springframework.mock.web.test.MockServletConfig)8 Test (org.junit.Test)6 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)4 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)4 MockServletContext (org.springframework.mock.web.test.MockServletContext)3 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)3 WebApplicationContext (org.springframework.web.context.WebApplicationContext)2 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)2 IOException (java.io.IOException)1 ServletConfig (javax.servlet.ServletConfig)1 HttpSession (javax.servlet.http.HttpSession)1 Before (org.junit.Before)1 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 ResponseStatusExceptionResolver (org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver)1 DefaultHandlerExceptionResolver (org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver)1