use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.
the class ResponseEntityExceptionHandlerTests method controllerAdviceWithNestedExceptionWithinDispatcherServlet.
@Test
public void controllerAdviceWithNestedExceptionWithinDispatcherServlet() throws Exception {
StaticWebApplicationContext ctx = new StaticWebApplicationContext();
ctx.registerSingleton("controller", NestedExceptionThrowingController.class);
ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
ctx.refresh();
DispatcherServlet servlet = new DispatcherServlet(ctx);
servlet.init(new MockServletConfig());
try {
servlet.service(this.servletRequest, this.servletResponse);
} catch (ServletException ex) {
boolean condition1 = ex.getCause() instanceof IllegalStateException;
assertThat(condition1).isTrue();
boolean condition = ex.getCause().getCause() instanceof ServletRequestBindingException;
assertThat(condition).isTrue();
}
}
use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.
the class ServletContextAwareProcessorTests method servletConfigAwareWithServletConfig.
@Test
public void servletConfigAwareWithServletConfig() {
ServletContext servletContext = new MockServletContext();
ServletConfig servletConfig = new MockServletConfig(servletContext);
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(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);
}
use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.
the class ServletContextAwareProcessorTests method servletContextAwareWithServletContextAndServletConfig.
@Test
public void servletContextAwareWithServletContextAndServletConfig() {
ServletContext servletContext = new MockServletContext();
ServletConfig servletConfig = new MockServletConfig(servletContext);
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext, 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);
}
use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.
the class ServletContextAwareProcessorTests method servletConfigAwareWithNullServletContextAndNonNullServletConfig.
@Test
public void servletConfigAwareWithNullServletContextAndNonNullServletConfig() {
ServletContext servletContext = new MockServletContext();
ServletConfig servletConfig = new MockServletConfig(servletContext);
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(null, 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);
}
use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.
the class HttpRequestHandlerTests method testHttpRequestHandlerServletPassThrough.
@Test
public void testHttpRequestHandlerServletPassThrough() throws Exception {
MockServletContext servletContext = new MockServletContext();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.getBeanFactory().registerSingleton("myHandler", (HttpRequestHandler) (req, res) -> {
assertThat(req).isSameAs(request);
assertThat(res).isSameAs(response);
String exception = request.getParameter("exception");
if ("ServletException".equals(exception)) {
throw new ServletException("test");
}
if ("IOException".equals(exception)) {
throw new IOException("test");
}
res.getWriter().write("myResponse");
});
wac.setServletContext(servletContext);
wac.refresh();
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
Servlet servlet = new HttpRequestHandlerServlet();
servlet.init(new MockServletConfig(servletContext, "myHandler"));
servlet.service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myResponse");
request.setParameter("exception", "ServletException");
assertThatExceptionOfType(ServletException.class).isThrownBy(() -> servlet.service(request, response)).withMessage("test");
request.setParameter("exception", "IOException");
assertThatIOException().isThrownBy(() -> servlet.service(request, response)).withMessage("test");
}
Aggregations