Search in sources :

Example 36 with MockServletContext

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

the class SimpleUrlHandlerMappingTests method handlerBeanNotFound.

@Test
@SuppressWarnings("resource")
public void handlerBeanNotFound() {
    MockServletContext sc = new MockServletContext("");
    XmlWebApplicationContext root = new XmlWebApplicationContext();
    root.setServletContext(sc);
    root.setConfigLocations("/org/springframework/web/servlet/handler/map1.xml");
    root.refresh();
    XmlWebApplicationContext wac = new XmlWebApplicationContext();
    wac.setParent(root);
    wac.setServletContext(sc);
    wac.setNamespace("map2err");
    wac.setConfigLocations("/org/springframework/web/servlet/handler/map2err.xml");
    assertThatExceptionOfType(FatalBeanException.class).isThrownBy(wac::refresh).withCauseInstanceOf(NoSuchBeanDefinitionException.class).satisfies(ex -> {
        NoSuchBeanDefinitionException cause = (NoSuchBeanDefinitionException) ex.getCause();
        assertThat(cause.getBeanName()).isEqualTo("mainControlle");
    });
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) FatalBeanException(org.springframework.beans.FatalBeanException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 37 with MockServletContext

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

the class TestValidator method loadBeanDefinitions.

private void loadBeanDefinitions(String fileName) {
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.appContext);
    ClassPathResource resource = new ClassPathResource(fileName, MessageBrokerBeanDefinitionParserTests.class);
    reader.loadBeanDefinitions(resource);
    this.appContext.setServletContext(new MockServletContext());
    this.appContext.refresh();
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext)

Example 38 with MockServletContext

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

the class RequestScopedControllerAdviceIntegrationTests method loadContextWithRequestScopedControllerAdvice.

// gh-23985
@Test
void loadContextWithRequestScopedControllerAdvice() {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(Config.class);
    assertThatCode(context::refresh).doesNotThrowAnyException();
    List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(context);
    assertThat(adviceBeans).hasSize(1);
    // 
    assertThat(adviceBeans.get(0)).returns(RequestScopedControllerAdvice.class, // 
    ControllerAdviceBean::getBeanType).returns(42, ControllerAdviceBean::getOrder);
    context.close();
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ControllerAdviceBean(org.springframework.web.method.ControllerAdviceBean) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 39 with MockServletContext

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

the class WebApplicationContextScopeTests method initApplicationContext.

private WebApplicationContext initApplicationContext(String scope) {
    MockServletContext sc = new MockServletContext();
    GenericWebApplicationContext ac = new GenericWebApplicationContext(sc);
    GenericBeanDefinition bd = new GenericBeanDefinition();
    bd.setBeanClass(DerivedTestBean.class);
    bd.setScope(scope);
    ac.registerBeanDefinition(NAME, bd);
    ac.refresh();
    return ac;
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext)

Example 40 with MockServletContext

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

the class RequestContextListenerTests method requestContextListenerWithSameThread.

@Test
public void requestContextListenerWithSameThread() {
    RequestContextListener listener = new RequestContextListener();
    MockServletContext context = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(context);
    request.setAttribute("test", "value");
    assertThat(RequestContextHolder.getRequestAttributes()).isNull();
    listener.requestInitialized(new ServletRequestEvent(context, request));
    assertThat(RequestContextHolder.getRequestAttributes()).isNotNull();
    assertThat(RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST)).isEqualTo("value");
    MockRunnable runnable = new MockRunnable();
    RequestContextHolder.getRequestAttributes().registerDestructionCallback("test", runnable, RequestAttributes.SCOPE_REQUEST);
    listener.requestDestroyed(new ServletRequestEvent(context, request));
    assertThat(RequestContextHolder.getRequestAttributes()).isNull();
    assertThat(runnable.wasExecuted()).isTrue();
}
Also used : MockRunnable(org.springframework.core.task.MockRunnable) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletRequestEvent(jakarta.servlet.ServletRequestEvent) 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