use of org.springframework.web.servlet.DispatcherServlet in project spring-framework by spring-projects.
the class DefaultMockMvcBuilderTests method dispatcherServletCustomizerProcessedInOrder.
@Test
public void dispatcherServletCustomizerProcessedInOrder() {
StubWebApplicationContext root = new StubWebApplicationContext(this.servletContext);
DefaultMockMvcBuilder builder = webAppContextSetup(root);
builder.addDispatcherServletCustomizer(ds -> ds.setContextId("test-id"));
builder.addDispatcherServletCustomizer(ds -> ds.setContextId("override-id"));
builder.dispatchOptions(true);
MockMvc mvc = builder.build();
DispatcherServlet ds = (DispatcherServlet) new DirectFieldAccessor(mvc).getPropertyValue("servlet");
assertEquals("override-id", ds.getContextId());
}
use of org.springframework.web.servlet.DispatcherServlet in project spring-boot by spring-projects.
the class SpringInitializer method onStartup.
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext();
webApplicationContext.register(SpringConfiguration.class);
servletContext.addServlet("dispatcherServlet", new DispatcherServlet(webApplicationContext)).addMapping("/*");
}
use of org.springframework.web.servlet.DispatcherServlet in project spring-framework by spring-projects.
the class ContextLoaderTests method testFrameworkServletWithDefaultLocation.
@Test
public void testFrameworkServletWithDefaultLocation() throws Exception {
DispatcherServlet servlet = new DispatcherServlet();
servlet.setContextClass(XmlWebApplicationContext.class);
try {
servlet.init(new MockServletConfig(new MockServletContext(""), "test"));
fail("Should have thrown BeanDefinitionStoreException");
} catch (BeanDefinitionStoreException ex) {
// expected
assertTrue(ex.getCause() instanceof IOException);
assertTrue(ex.getCause().getMessage().contains("/WEB-INF/test-servlet.xml"));
}
}
use of org.springframework.web.servlet.DispatcherServlet in project spring-framework by spring-projects.
the class AnnotationConfigDispatcherServletInitializerTests method rootContextOnly.
// SPR-11357
@Test
public void rootContextOnly() throws ServletException {
initializer = new MyAnnotationConfigDispatcherServletInitializer() {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { MyConfiguration.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
};
initializer.onStartup(servletContext);
DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
servlet.init(new MockServletConfig(this.servletContext));
WebApplicationContext wac = servlet.getWebApplicationContext();
((AnnotationConfigWebApplicationContext) wac).refresh();
assertTrue(wac.containsBean("bean"));
assertTrue(wac.getBean("bean") instanceof MyBean);
}
use of org.springframework.web.servlet.DispatcherServlet in project spring-boot by spring-projects.
the class DispatcherServletAutoConfigurationTests method dispatcherServletDefaultConfig.
@Test
public void dispatcherServletDefaultConfig() {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(DispatcherServletAutoConfiguration.class);
this.context.refresh();
DispatcherServlet bean = this.context.getBean(DispatcherServlet.class);
assertThat(bean).extracting("throwExceptionIfNoHandlerFound").containsExactly(false);
assertThat(bean).extracting("dispatchOptionsRequest").containsExactly(true);
assertThat(bean).extracting("dispatchTraceRequest").containsExactly(false);
assertThat(new DirectFieldAccessor(this.context.getBean("dispatcherServletRegistration")).getPropertyValue("loadOnStartup")).isEqualTo(-1);
}
Aggregations