Search in sources :

Example 21 with MockServletConfig

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

the class DispatcherServletTests method notDetectAllHandlerMappings.

@Test
public void notDetectAllHandlerMappings() throws ServletException, IOException {
    DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
    complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
    complexDispatcherServlet.setNamespace("test");
    complexDispatcherServlet.setDetectAllHandlerMappings(false);
    complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    complexDispatcherServlet.service(request, response);
    assertTrue(response.getStatus() == HttpServletResponse.SC_NOT_FOUND);
}
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 22 with MockServletConfig

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

the class DispatcherServletTests method handlerNotMappedWithAutodetect.

@Test
public void handlerNotMappedWithAutodetect() throws ServletException, IOException {
    DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
    // no parent
    complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
    complexDispatcherServlet.setNamespace("test");
    complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", URL_KNOWN_ONLY_PARENT);
    MockHttpServletResponse response = new MockHttpServletResponse();
    complexDispatcherServlet.service(request, response);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus());
}
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 23 with MockServletConfig

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

the class ServletContextAwareProcessorTests method servletContextAwareWithServletConfig.

@Test
public void servletContextAwareWithServletConfig() {
    ServletContext servletContext = new MockServletContext();
    ServletConfig servletConfig = new MockServletConfig(servletContext);
    ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletConfig);
    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) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 24 with MockServletConfig

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

the class ServletContextAwareProcessorTests method servletContextAwareWithNullServletContextAndNonNullServletConfig.

@Test
public void servletContextAwareWithNullServletContextAndNonNullServletConfig() {
    ServletContext servletContext = new MockServletContext();
    ServletConfig servletConfig = new MockServletConfig(servletContext);
    ServletContextAwareProcessor processor = new ServletContextAwareProcessor(null, servletConfig);
    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) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 25 with MockServletConfig

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

the class ServletContextAwareProcessorTests method servletConfigAwareWithServletContextAndServletConfig.

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

Aggregations

MockServletConfig (org.springframework.mock.web.test.MockServletConfig)29 Test (org.junit.Test)24 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)14 MockServletContext (org.springframework.mock.web.test.MockServletContext)13 ServletConfig (javax.servlet.ServletConfig)8 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)7 ServletContext (javax.servlet.ServletContext)6 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)6 WebApplicationContext (org.springframework.web.context.WebApplicationContext)5 ServletException (javax.servlet.ServletException)4 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)4 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)3 IOException (java.io.IOException)2 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)2 SimpleTraceInterceptor (org.springframework.aop.interceptor.SimpleTraceInterceptor)2 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)2 ServletConfigAwareBean (org.springframework.web.context.ServletConfigAwareBean)2 ServletContextAwareBean (org.springframework.web.context.ServletContextAwareBean)2 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)2