Search in sources :

Example 1 with MockServletConfig

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();
    }
}
Also used : ServletException(jakarta.servlet.ServletException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 2 with MockServletConfig

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);
}
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 3 with MockServletConfig

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);
}
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 4 with MockServletConfig

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);
}
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 5 with MockServletConfig

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");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WebApplicationContext(org.springframework.web.context.WebApplicationContext) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) Test(org.junit.jupiter.api.Test) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Servlet(jakarta.servlet.Servlet) HttpRequestHandler(org.springframework.web.HttpRequestHandler) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletException(jakarta.servlet.ServletException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Servlet(jakarta.servlet.Servlet) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) IOException(java.io.IOException) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) 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