Search in sources :

Example 56 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project spring-boot by spring-projects.

the class HypermediaAutoConfigurationTests method entityLinksCreated.

@Test
public void entityLinksCreated() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(BaseConfig.class);
    this.context.refresh();
    EntityLinks discoverers = this.context.getBean(EntityLinks.class);
    assertThat(discoverers).isNotNull();
}
Also used : EntityLinks(org.springframework.hateoas.EntityLinks) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 57 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project spring-boot by spring-projects.

the class HypermediaAutoConfigurationTests method jacksonConfigurationIsAppliedToTheHalObjectMapper.

@Test
public void jacksonConfigurationIsAppliedToTheHalObjectMapper() {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(BaseConfig.class);
    EnvironmentTestUtils.addEnvironment(this.context, "spring.jackson.serialization.INDENT_OUTPUT:true");
    this.context.refresh();
    ObjectMapper objectMapper = this.context.getBean("_halObjectMapper", ObjectMapper.class);
    assertThat(objectMapper.getSerializationConfig().isEnabled(SerializationFeature.INDENT_OUTPUT)).isTrue();
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 58 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project spring-boot by spring-projects.

the class ThymeleafAutoConfigurationTests method createLayoutFromConfigClass.

@Test
public void createLayoutFromConfigClass() throws Exception {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    MockServletContext servletContext = new MockServletContext();
    context.setServletContext(servletContext);
    context.refresh();
    ThymeleafView view = (ThymeleafView) context.getBean(ThymeleafViewResolver.class).resolveViewName("view", Locale.UK);
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
    view.render(Collections.singletonMap("foo", "bar"), request, response);
    String result = response.getContentAsString();
    assertThat(result).contains("<title>Content</title>");
    assertThat(result).contains("<span>bar</span>");
    context.close();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ThymeleafView(org.thymeleaf.spring5.view.ThymeleafView) Matchers.containsString(org.hamcrest.Matchers.containsString) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 59 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project spring-boot by spring-projects.

the class DispatcherServletAutoConfigurationTests method renamesMultipartResolver.

@Test
public void renamesMultipartResolver() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(MultipartResolverConfiguration.class, DispatcherServletAutoConfiguration.class);
    this.context.refresh();
    DispatcherServlet dispatcherServlet = this.context.getBean(DispatcherServlet.class);
    dispatcherServlet.onApplicationEvent(new ContextRefreshedEvent(this.context));
    assertThat(dispatcherServlet.getMultipartResolver()).isInstanceOf(MockMultipartResolver.class);
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) Test(org.junit.Test)

Example 60 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project spring-boot by spring-projects.

the class DispatcherServletAutoConfigurationTests method registrationOverrideWithDispatcherServletWrongName.

// If a DispatcherServlet instance is registered with a name different
// from the default one, we're registering one anyway
@Test
public void registrationOverrideWithDispatcherServletWrongName() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.register(CustomDispatcherServletWrongName.class, DispatcherServletAutoConfiguration.class);
    this.context.setServletContext(new MockServletContext());
    this.context.refresh();
    ServletRegistrationBean<?> registration = this.context.getBean(ServletRegistrationBean.class);
    assertThat(registration.getUrlMappings().toString()).isEqualTo("[/]");
    assertThat(registration.getServletName()).isEqualTo("dispatcherServlet");
    assertThat(this.context.getBeanNamesForType(DispatcherServlet.class).length).isEqualTo(2);
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Aggregations

MockServletContext (org.springframework.mock.web.MockServletContext)186 Test (org.junit.Test)135 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)82 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)80 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)74 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)54 MockRequestContext (org.springframework.webflow.test.MockRequestContext)46 Event (org.springframework.webflow.execution.Event)18 Before (org.junit.Before)16 EventFactorySupport (org.springframework.webflow.action.EventFactorySupport)14 MockMvc (org.springframework.test.web.servlet.MockMvc)13 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)12 MockFilterConfig (org.springframework.mock.web.MockFilterConfig)9 WebStatFilter (com.alibaba.druid.support.http.WebStatFilter)8 MockFilterChain (org.springframework.mock.web.MockFilterChain)8 MockHttpSession (org.springframework.mock.web.MockHttpSession)8 Map (java.util.Map)7 Credential (org.apereo.cas.authentication.Credential)7 Cookie (javax.servlet.http.Cookie)6 FilterChainProxy (org.springframework.security.web.FilterChainProxy)6