Search in sources :

Example 6 with MockServletContext

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

the class DelegatingWebMvcConfigurationIntegrationTests method load.

private void load(Consumer<GenericWebApplicationContext> context) {
    GenericWebApplicationContext webContext = new GenericWebApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(webContext);
    webContext.setServletContext(new MockServletContext());
    context.accept(webContext);
    webContext.registerBean(DelegatingWebMvcConfiguration.class);
    webContext.refresh();
    this.context = webContext;
}
Also used : GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext)

Example 7 with MockServletContext

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

the class DispatcherServletTests method withNoView.

@Test
public void withNoView() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/noview.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    complexDispatcherServlet.service(request, response);
    assertThat(response.getForwardedUrl()).isEqualTo("noview.jsp");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 8 with MockServletContext

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

the class DispatcherServletTests method dispatcherServletContextRefresh.

@Test
public void dispatcherServletContextRefresh() 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();
    ((ConfigurableApplicationContext) servlet.getWebApplicationContext()).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(contextBean != contextBean2).isTrue();
    assertThat(configBean != configBean2).isTrue();
    MultipartResolver multipartResolver2 = servlet.getMultipartResolver();
    assertThat(multipartResolver != multipartResolver2).isTrue();
    servlet.destroy();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) 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 9 with MockServletContext

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

the class DispatcherServletTests method withNoViewNested.

@Test
public void withNoViewNested() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/noview/simple.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    complexDispatcherServlet.service(request, response);
    assertThat(response.getForwardedUrl()).isEqualTo("noview/simple.jsp");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 10 with MockServletContext

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

the class ResourceHttpRequestHandlerTests method getMediaTypeWithFavorPathExtensionOff.

// SPR-14577
@Test
@SuppressWarnings("deprecation")
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
    ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
    factory.setFavorPathExtension(false);
    factory.afterPropertiesSet();
    ContentNegotiationManager manager = factory.getObject();
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
    handler.setServletContext(new MockServletContext());
    handler.setLocations(paths);
    handler.setContentNegotiationManager(manager);
    handler.afterPropertiesSet();
    this.request.addHeader("Accept", "application/json,text/plain,*/*");
    this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html");
    handler.handleRequest(this.request, this.response);
    assertThat(this.response.getContentType()).isEqualTo("text/html");
}
Also used : ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Aggregations

MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)138 Test (org.junit.jupiter.api.Test)112 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)44 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)38 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)24 ServletContext (jakarta.servlet.ServletContext)22 ServletContextEvent (jakarta.servlet.ServletContextEvent)21 BeforeEach (org.junit.jupiter.api.BeforeEach)17 WebApplicationContext (org.springframework.web.context.WebApplicationContext)17 MockFilterConfig (org.springframework.web.testfixture.servlet.MockFilterConfig)14 MockServletConfig (org.springframework.web.testfixture.servlet.MockServletConfig)13 Resource (org.springframework.core.io.Resource)12 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)12 HashMap (java.util.HashMap)11 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)11 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)10 TestBean (org.springframework.beans.testfixture.beans.TestBean)9 ClassPathResource (org.springframework.core.io.ClassPathResource)9 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)9 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)7