Search in sources :

Example 21 with MockServletConfig

use of org.springframework.web.testfixture.servlet.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);
    assertThat(response.getStatus() == HttpServletResponse.SC_NOT_FOUND).isTrue();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 22 with MockServletConfig

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

the class DispatcherServletTests method dispatcherServletRefresh.

@Test
public void dispatcherServletRefresh() 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");
    ServletConfigAwareBean configBean = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean");
    assertThat(contextBean.getServletContext()).isSameAs(servletContext);
    assertThat(configBean.getServletConfig()).isSameAs(servlet.getServletConfig());
    MultipartResolver multipartResolver = servlet.getMultipartResolver();
    assertThat(multipartResolver).isNotNull();
    servlet.refresh();
    ServletContextAwareBean contextBean2 = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean");
    ServletConfigAwareBean configBean2 = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean");
    assertThat(contextBean2.getServletContext()).isSameAs(servletContext);
    assertThat(configBean2.getServletConfig()).isSameAs(servlet.getServletConfig());
    assertThat(contextBean2).isNotSameAs(contextBean);
    assertThat(configBean2).isNotSameAs(configBean);
    MultipartResolver multipartResolver2 = servlet.getMultipartResolver();
    assertThat(multipartResolver2).isNotSameAs(multipartResolver);
    servlet.destroy();
}
Also used : ServletContextAwareBean(org.springframework.web.context.ServletContextAwareBean) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MultipartResolver(org.springframework.web.multipart.MultipartResolver) ServletConfigAwareBean(org.springframework.web.context.ServletConfigAwareBean) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 23 with MockServletConfig

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

the class DispatcherServletTests method detectAllHandlerAdapters.

@Test
public void detectAllHandlerAdapters() throws ServletException, IOException {
    DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
    complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
    complexDispatcherServlet.setNamespace("test");
    complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/servlet.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    complexDispatcherServlet.service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("body");
    request = new MockHttpServletRequest(getServletContext(), "GET", "/form.do");
    response = new MockHttpServletResponse();
    complexDispatcherServlet.service(request, response);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 24 with MockServletConfig

use of org.springframework.web.testfixture.servlet.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();
    assertThatExceptionOfType(ServletException.class).isThrownBy(() -> complexDispatcherServlet.service(request, response)).withMessageContaining("No adapter for handler");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 25 with MockServletConfig

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

the class ContextLoaderTests method frameworkServletWithCustomLocation.

@Test
void frameworkServletWithCustomLocation() 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"));
    assertThat(servlet.getWebApplicationContext().containsBean("kerry")).isTrue();
    assertThat(servlet.getWebApplicationContext().containsBean("kerryX")).isTrue();
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) 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