use of org.springframework.web.testfixture.servlet.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);
assertThat(response.getContentType()).isEqualTo(AbstractView.DEFAULT_CONTENT_TYPE);
}
use of org.springframework.web.testfixture.servlet.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);
assertThat(response.getContentType()).isEqualTo("myContentType");
}
use of org.springframework.web.testfixture.servlet.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");
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> fv.setApplicationContext(wac)).withMessageContaining("FreeMarkerConfig");
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class JythonScriptTemplateTests method setup.
@BeforeEach
public void setup() {
this.webAppContext = mock(WebApplicationContext.class);
this.servletContext = new MockServletContext();
this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class NashornScriptTemplateTests method setup.
@BeforeEach
public void setup() {
this.webAppContext = mock(WebApplicationContext.class);
this.servletContext = new MockServletContext();
this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
}
Aggregations