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