Search in sources :

Example 26 with MockServletConfig

use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.

the class ContextLoaderTests method frameworkServletWithDefaultLocation.

@Test
void frameworkServletWithDefaultLocation() throws Exception {
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.setContextClass(XmlWebApplicationContext.class);
    assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> servlet.init(new MockServletConfig(new MockServletContext(""), "test"))).havingCause().isInstanceOf(IOException.class).withMessageContaining("/WEB-INF/test-servlet.xml");
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) IOException(java.io.IOException) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 27 with MockServletConfig

use of org.springframework.web.testfixture.servlet.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();
    assertThat(bean.getServletContext()).isNull();
    processor.postProcessBeforeInitialization(bean, "testBean");
    assertThat(bean.getServletContext()).as("ServletContext should have been set").isNotNull();
    assertThat(bean.getServletContext()).isEqualTo(servletContext);
}
Also used : ServletContextAwareProcessor(org.springframework.web.context.support.ServletContextAwareProcessor) ServletConfig(jakarta.servlet.ServletConfig) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) ServletContext(jakarta.servlet.ServletContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 28 with MockServletConfig

use of org.springframework.web.testfixture.servlet.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();
    assertThat(bean.getServletContext()).isNull();
    processor.postProcessBeforeInitialization(bean, "testBean");
    assertThat(bean.getServletContext()).as("ServletContext should have been set").isNotNull();
    assertThat(bean.getServletContext()).isEqualTo(servletContext);
}
Also used : ServletContextAwareProcessor(org.springframework.web.context.support.ServletContextAwareProcessor) ServletConfig(jakarta.servlet.ServletConfig) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) ServletContext(jakarta.servlet.ServletContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 29 with MockServletConfig

use of org.springframework.web.testfixture.servlet.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();
    assertThat(bean.getServletConfig()).isNull();
    processor.postProcessBeforeInitialization(bean, "testBean");
    assertThat(bean.getServletConfig()).as("ServletConfig should have been set").isNotNull();
    assertThat(bean.getServletConfig()).isEqualTo(servletConfig);
}
Also used : ServletContextAwareProcessor(org.springframework.web.context.support.ServletContextAwareProcessor) ServletConfig(jakarta.servlet.ServletConfig) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) ServletContext(jakarta.servlet.ServletContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 30 with MockServletConfig

use of org.springframework.web.testfixture.servlet.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();
    assertThat(wac.containsBean("bean")).isTrue();
    boolean condition = wac.getBean("bean") instanceof MyBean;
    assertThat(condition).isTrue();
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.jupiter.api.Test)

Aggregations

MockServletConfig (org.springframework.web.testfixture.servlet.MockServletConfig)30 Test (org.junit.jupiter.api.Test)23 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)13 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)13 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)13 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)10 ServletConfig (jakarta.servlet.ServletConfig)7 ServletContext (jakarta.servlet.ServletContext)6 WebApplicationContext (org.springframework.web.context.WebApplicationContext)6 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)6 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)5 ServletException (jakarta.servlet.ServletException)3 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)3 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)3 PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)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