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);
}
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());
}
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());
}
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");
}
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);
}
}
Aggregations