Search in sources :

Example 46 with MockServletContext

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

the class ContentNegotiatingViewResolverTests method resolveViewNameRedirectView.

@Test
public void resolveViewNameRedirectView() throws Exception {
    request.addHeader("Accept", "application/json");
    request.setRequestURI("/test");
    StaticWebApplicationContext webAppContext = new StaticWebApplicationContext();
    webAppContext.setServletContext(new MockServletContext());
    webAppContext.refresh();
    UrlBasedViewResolver urlViewResolver = new InternalResourceViewResolver();
    urlViewResolver.setApplicationContext(webAppContext);
    ViewResolver xmlViewResolver = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.<ViewResolver>asList(xmlViewResolver, urlViewResolver));
    View xmlView = mock(View.class, "application_xml");
    View jsonView = mock(View.class, "application_json");
    viewResolver.setDefaultViews(Arrays.asList(jsonView));
    viewResolver.afterPropertiesSet();
    String viewName = "redirect:anotherTest";
    Locale locale = Locale.ENGLISH;
    given(xmlViewResolver.resolveViewName(viewName, locale)).willReturn(xmlView);
    given(jsonView.getContentType()).willReturn("application/json");
    View actualView = viewResolver.resolveViewName(viewName, locale);
    assertEquals("Invalid view", RedirectView.class, actualView.getClass());
}
Also used : Locale(java.util.Locale) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 47 with MockServletContext

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

the class ScriptTemplateViewTests method resourceLoaderPath.

// SPR-14210
@Test
public void resourceLoaderPath() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    this.wac.setServletContext(servletContext);
    this.wac.refresh();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
    MockHttpServletResponse response = new MockHttpServletResponse();
    Map<String, Object> model = new HashMap<>();
    InvocableScriptEngine engine = mock(InvocableScriptEngine.class);
    when(engine.invokeFunction(any(), any(), any(), any())).thenReturn("foo");
    this.view.setEngine(engine);
    this.view.setRenderFunction("render");
    this.view.setApplicationContext(this.wac);
    this.view.setUrl("org/springframework/web/servlet/view/script/empty.txt");
    this.view.render(model, request, response);
    assertEquals("foo", response.getContentAsString());
    response = new MockHttpServletResponse();
    this.view.setResourceLoaderPath("classpath:org/springframework/web/servlet/view/script/");
    this.view.setUrl("empty.txt");
    this.view.render(model, request, response);
    assertEquals("foo", response.getContentAsString());
    response = new MockHttpServletResponse();
    this.view.setResourceLoaderPath("classpath:org/springframework/web/servlet/view/script");
    this.view.setUrl("empty.txt");
    this.view.render(model, request, response);
    assertEquals("foo", response.getContentAsString());
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 48 with MockServletContext

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

the class TilesConfigurerTests method simpleBootstrap.

@Test
public void simpleBootstrap() {
    MockServletContext servletContext = new MockServletContext();
    TilesConfigurer tc = new TilesConfigurer();
    tc.setDefinitions("/org/springframework/web/servlet/view/tiles3/tiles-definitions.xml");
    tc.setCheckRefresh(true);
    tc.setServletContext(servletContext);
    tc.afterPropertiesSet();
    ApplicationContext tilesContext = ServletUtil.getApplicationContext(servletContext);
    BasicTilesContainer container = (BasicTilesContainer) TilesAccess.getContainer(tilesContext);
    Request requestContext = new ServletRequest(container.getApplicationContext(), new MockHttpServletRequest(), new MockHttpServletResponse());
    assertNotNull(container.getDefinitionsFactory().getDefinition("test", requestContext));
    tc.destroy();
}
Also used : ServletRequest(org.apache.tiles.request.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ApplicationContext(org.apache.tiles.request.ApplicationContext) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) BasicTilesContainer(org.apache.tiles.impl.BasicTilesContainer) ServletRequest(org.apache.tiles.request.servlet.ServletRequest) Request(org.apache.tiles.request.Request) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 49 with MockServletContext

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

the class GroovyMarkupViewTests method setup.

@Before
public void setup() {
    this.webAppContext = mock(WebApplicationContext.class);
    this.servletContext = new MockServletContext();
    this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
}
Also used : MockServletContext(org.springframework.mock.web.test.MockServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Before(org.junit.Before)

Example 50 with MockServletContext

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

the class JRubyScriptTemplateTests method setup.

@Before
public void setup() {
    this.webAppContext = mock(WebApplicationContext.class);
    this.servletContext = new MockServletContext();
    this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
}
Also used : MockServletContext(org.springframework.mock.web.test.MockServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Before(org.junit.Before)

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