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();
}
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();
}
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);
}
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");
}
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();
}
Aggregations