Search in sources :

Example 6 with MockServletWebServerFactory

use of org.springframework.boot.web.servlet.server.MockServletWebServerFactory in project spring-boot by spring-projects.

the class AnnotationConfigServletWebServerApplicationContextTests method verifyContext.

private void verifyContext() {
    MockServletWebServerFactory factory = this.context.getBean(MockServletWebServerFactory.class);
    Servlet servlet = this.context.getBean(Servlet.class);
    verify(factory.getServletContext()).addServlet("servlet", servlet);
}
Also used : MockServletWebServerFactory(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) Servlet(javax.servlet.Servlet) MockServlet(org.springframework.boot.testutil.MockServlet) GenericServlet(javax.servlet.GenericServlet)

Example 7 with MockServletWebServerFactory

use of org.springframework.boot.web.servlet.server.MockServletWebServerFactory in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method singleServletBean.

@Test
public void singleServletBean() throws Exception {
    addWebServerFactoryBean();
    Servlet servlet = mock(Servlet.class);
    this.context.registerBeanDefinition("servletBean", beanDefinition(servlet));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    verify(factory.getServletContext()).addServlet("servletBean", servlet);
    verify(factory.getRegisteredServlet(0).getRegistration()).addMapping("/");
}
Also used : MockServletWebServerFactory(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) Servlet(javax.servlet.Servlet) Test(org.junit.Test)

Example 8 with MockServletWebServerFactory

use of org.springframework.boot.web.servlet.server.MockServletWebServerFactory in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method orderedBeanInsertedCorrectly.

@Test
public void orderedBeanInsertedCorrectly() throws Exception {
    addWebServerFactoryBean();
    OrderedFilter filter = new OrderedFilter();
    this.context.registerBeanDefinition("filterBean", beanDefinition(filter));
    FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
    registration.setFilter(mock(Filter.class));
    registration.setOrder(100);
    this.context.registerBeanDefinition("filterRegistrationBean", beanDefinition(registration));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    verify(factory.getServletContext()).addFilter("filterBean", filter);
    verify(factory.getServletContext()).addFilter("object", registration.getFilter());
    assertThat(factory.getRegisteredFilter(0).getFilter()).isEqualTo(filter);
}
Also used : MockServletWebServerFactory(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) Filter(javax.servlet.Filter) FilterRegistrationBean(org.springframework.boot.web.servlet.FilterRegistrationBean) Test(org.junit.Test)

Example 9 with MockServletWebServerFactory

use of org.springframework.boot.web.servlet.server.MockServletWebServerFactory in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method servletAndFilterBeans.

@Test
public void servletAndFilterBeans() throws Exception {
    addWebServerFactoryBean();
    Servlet servlet = mock(Servlet.class);
    Filter filter1 = mock(Filter.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) filter1).getOrder()).willReturn(1);
    Filter filter2 = mock(Filter.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) filter2).getOrder()).willReturn(2);
    this.context.registerBeanDefinition("servletBean", beanDefinition(servlet));
    this.context.registerBeanDefinition("filterBean2", beanDefinition(filter2));
    this.context.registerBeanDefinition("filterBean1", beanDefinition(filter1));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    ServletContext servletContext = factory.getServletContext();
    InOrder ordered = inOrder(servletContext);
    verify(factory.getServletContext()).addServlet("servletBean", servlet);
    verify(factory.getRegisteredServlet(0).getRegistration()).addMapping("/");
    ordered.verify(factory.getServletContext()).addFilter("filterBean1", filter1);
    ordered.verify(factory.getServletContext()).addFilter("filterBean2", filter2);
    verify(factory.getRegisteredFilter(0).getRegistration()).addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
    verify(factory.getRegisteredFilter(1).getRegistration()).addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
}
Also used : MockServletWebServerFactory(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) InOrder(org.mockito.InOrder) Filter(javax.servlet.Filter) Ordered(org.springframework.core.Ordered) Servlet(javax.servlet.Servlet) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 10 with MockServletWebServerFactory

use of org.springframework.boot.web.servlet.server.MockServletWebServerFactory in project spring-boot by spring-projects.

the class ServletWebServerApplicationContextTests method startRegistrations.

@Test
public void startRegistrations() throws Exception {
    addWebServerFactoryBean();
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    // Ensure that the context has been setup
    assertThat(this.context.getServletContext()).isEqualTo(factory.getServletContext());
    verify(factory.getServletContext()).setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
    // Ensure WebApplicationContextUtils.registerWebApplicationScopes was called
    assertThat(this.context.getBeanFactory().getRegisteredScope(WebApplicationContext.SCOPE_SESSION)).isInstanceOf(SessionScope.class);
    // Ensure WebApplicationContextUtils.registerEnvironmentBeans was called
    assertThat(this.context.containsBean(WebApplicationContext.SERVLET_CONTEXT_BEAN_NAME)).isTrue();
}
Also used : MockServletWebServerFactory(org.springframework.boot.web.servlet.server.MockServletWebServerFactory) Test(org.junit.Test)

Aggregations

MockServletWebServerFactory (org.springframework.boot.web.servlet.server.MockServletWebServerFactory)10 Servlet (javax.servlet.Servlet)7 Test (org.junit.Test)7 ServletContext (javax.servlet.ServletContext)3 InOrder (org.mockito.InOrder)3 Ordered (org.springframework.core.Ordered)3 Filter (javax.servlet.Filter)2 GenericServlet (javax.servlet.GenericServlet)1 MockServlet (org.springframework.boot.testutil.MockServlet)1 FilterRegistrationBean (org.springframework.boot.web.servlet.FilterRegistrationBean)1 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)1 FrameworkServlet (org.springframework.web.servlet.FrameworkServlet)1