Search in sources :

Example 26 with MockServletConfig

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

the class DispatcherServletTests method notDetectAllViewResolvers.

@Test
public void notDetectAllViewResolvers() throws ServletException, IOException {
    DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
    complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
    complexDispatcherServlet.setNamespace("test");
    complexDispatcherServlet.setDetectAllViewResolvers(false);
    complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    try {
        complexDispatcherServlet.service(request, response);
        fail("Should have thrown ServletException");
    } catch (ServletException ex) {
        // expected
        assertTrue(ex.getMessage().indexOf("failed0") != -1);
    }
}
Also used : ServletException(javax.servlet.ServletException) 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 27 with MockServletConfig

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

the class DispatcherServletTests method notDetectAllHandlerExceptionResolvers.

@Test
public void notDetectAllHandlerExceptionResolvers() throws ServletException, IOException {
    DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
    complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
    complexDispatcherServlet.setNamespace("test");
    complexDispatcherServlet.setDetectAllHandlerExceptionResolvers(false);
    complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    try {
        complexDispatcherServlet.service(request, response);
        fail("Should have thrown ServletException");
    } catch (ServletException ex) {
        // expected
        assertTrue(ex.getMessage().indexOf("No adapter for handler") != -1);
    }
}
Also used : ServletException(javax.servlet.ServletException) 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 28 with MockServletConfig

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

the class ContextLoaderTests method testFrameworkServletWithDefaultLocation.

@Test
public void testFrameworkServletWithDefaultLocation() throws Exception {
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.setContextClass(XmlWebApplicationContext.class);
    try {
        servlet.init(new MockServletConfig(new MockServletContext(""), "test"));
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
        // expected
        assertTrue(ex.getCause() instanceof IOException);
        assertTrue(ex.getCause().getMessage().contains("/WEB-INF/test-servlet.xml"));
    }
}
Also used : BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) IOException(java.io.IOException) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 29 with MockServletConfig

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

the class AnnotationConfigDispatcherServletInitializerTests method rootContextOnly.

// SPR-11357
@Test
public void rootContextOnly() throws ServletException {
    initializer = new MyAnnotationConfigDispatcherServletInitializer() {

        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class<?>[] { MyConfiguration.class };
        }

        @Override
        protected Class<?>[] getServletConfigClasses() {
            return null;
        }
    };
    initializer.onStartup(servletContext);
    DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
    servlet.init(new MockServletConfig(this.servletContext));
    WebApplicationContext wac = servlet.getWebApplicationContext();
    ((AnnotationConfigWebApplicationContext) wac).refresh();
    assertTrue(wac.containsBean("bean"));
    assertTrue(wac.getBean("bean") instanceof MyBean);
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) 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