Search in sources :

Example 41 with MockServletContext

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

the class FreeMarkerViewTests method noFreeMarkerConfig.

@Test
public void noFreeMarkerConfig() throws Exception {
    FreeMarkerView fv = new FreeMarkerView();
    WebApplicationContext wac = mock(WebApplicationContext.class);
    given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(new HashMap<>());
    given(wac.getServletContext()).willReturn(new MockServletContext());
    fv.setUrl("anythingButNull");
    exception.expect(ApplicationContextException.class);
    exception.expectMessage(containsString("FreeMarkerConfig"));
    fv.setApplicationContext(wac);
}
Also used : MockServletContext(org.springframework.mock.web.test.MockServletContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 42 with MockServletContext

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

the class FreeMarkerViewTests method keepExistingContentType.

@Test
public void keepExistingContentType() throws Exception {
    FreeMarkerView fv = new FreeMarkerView();
    WebApplicationContext wac = mock(WebApplicationContext.class);
    MockServletContext sc = new MockServletContext();
    Map<String, FreeMarkerConfig> configs = new HashMap<>();
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setConfiguration(new TestConfiguration());
    configs.put("configurer", configurer);
    given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
    given(wac.getServletContext()).willReturn(sc);
    fv.setUrl("templateName");
    fv.setApplicationContext(wac);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.US);
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    HttpServletResponse response = new MockHttpServletResponse();
    response.setContentType("myContentType");
    Map<String, Object> model = new HashMap<>();
    model.put("myattr", "myvalue");
    fv.render(model, request, response);
    assertEquals("myContentType", response.getContentType());
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MockServletContext(org.springframework.mock.web.test.MockServletContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 43 with MockServletContext

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

the class FreeMarkerViewTests method validTemplateName.

@Test
public void validTemplateName() throws Exception {
    FreeMarkerView fv = new FreeMarkerView();
    WebApplicationContext wac = mock(WebApplicationContext.class);
    MockServletContext sc = new MockServletContext();
    Map<String, FreeMarkerConfig> configs = new HashMap<>();
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setConfiguration(new TestConfiguration());
    configs.put("configurer", configurer);
    given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
    given(wac.getServletContext()).willReturn(sc);
    fv.setUrl("templateName");
    fv.setApplicationContext(wac);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.US);
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    HttpServletResponse response = new MockHttpServletResponse();
    Map<String, Object> model = new HashMap<>();
    model.put("myattr", "myvalue");
    fv.render(model, request, response);
    assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType());
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MockServletContext(org.springframework.mock.web.test.MockServletContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 44 with MockServletContext

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

the class RedirectViewTests method updateTargetUrl.

@Test
public void updateTargetUrl() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class);
    wac.setServletContext(new MockServletContext());
    wac.refresh();
    RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
    wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);
    RedirectView rv = new RedirectView();
    // Init RedirectView with WebAppCxt
    rv.setApplicationContext(wac);
    rv.setUrl("/path");
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123");
    rv.render(new ModelMap(), request, response);
    verify(mockProcessor).processUrl(request, "/path");
}
Also used : RequestDataValueProcessor(org.springframework.web.servlet.support.RequestDataValueProcessor) RequestDataValueProcessorWrapper(org.springframework.web.servlet.support.RequestDataValueProcessorWrapper) ModelMap(org.springframework.ui.ModelMap) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 45 with MockServletContext

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

the class RedirectViewTests method updateTargetUrlWithContextLoader.

@Test
public void updateTargetUrlWithContextLoader() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class);
    MockServletContext servletContext = new MockServletContext();
    ContextLoader contextLoader = new ContextLoader(wac);
    contextLoader.initWebApplicationContext(servletContext);
    try {
        RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
        wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);
        RedirectView rv = new RedirectView();
        rv.setUrl("/path");
        given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123");
        rv.render(new ModelMap(), request, response);
        verify(mockProcessor).processUrl(request, "/path");
    } finally {
        contextLoader.closeWebApplicationContext(servletContext);
    }
}
Also used : ContextLoader(org.springframework.web.context.ContextLoader) RequestDataValueProcessor(org.springframework.web.servlet.support.RequestDataValueProcessor) RequestDataValueProcessorWrapper(org.springframework.web.servlet.support.RequestDataValueProcessorWrapper) ModelMap(org.springframework.ui.ModelMap) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Aggregations

MockServletContext (org.springframework.mock.web.test.MockServletContext)152 Test (org.junit.Test)120 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)56 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)50 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)41 ServletContext (javax.servlet.ServletContext)22 ServletContextEvent (javax.servlet.ServletContextEvent)21 HashMap (java.util.HashMap)18 Before (org.junit.Before)18 HttpServletResponse (javax.servlet.http.HttpServletResponse)16 MockFilterConfig (org.springframework.mock.web.test.MockFilterConfig)16 TestBean (org.springframework.tests.sample.beans.TestBean)15 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)14 MockServletConfig (org.springframework.mock.web.test.MockServletConfig)13 View (org.springframework.web.servlet.View)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 WebApplicationContext (org.springframework.web.context.WebApplicationContext)12 Resource (org.springframework.core.io.Resource)11 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)10 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)9