use of org.springframework.test.web.servlet.MockMvc in project spring-framework by spring-projects.
the class MockMvcClientHttpRequestFactoryTests method setup.
@Before
public void setup() {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).alwaysExpect(status().isOk()).build();
this.restTemplate = new RestTemplate(new MockMvcClientHttpRequestFactory(mockMvc));
}
use of org.springframework.test.web.servlet.MockMvc in project spring-framework by spring-projects.
the class HtmlUnitRequestBuilderTests method mergeDoesNotCorruptPathInfoOnParent.
// SPR-14584
@Test
public void mergeDoesNotCorruptPathInfoOnParent() throws Exception {
String pathInfo = "/foo/bar";
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new HelloController()).defaultRequest(get("/")).build();
assertThat(mockMvc.perform(get(pathInfo)).andReturn().getRequest().getPathInfo(), equalTo(pathInfo));
mockMvc.perform(requestBuilder);
assertThat(mockMvc.perform(get(pathInfo)).andReturn().getRequest().getPathInfo(), equalTo(pathInfo));
}
use of org.springframework.test.web.servlet.MockMvc in project spring-framework by spring-projects.
the class HtmlUnitRequestBuilderTests method mergeRequestAttribute.
@Test
public void mergeRequestAttribute() throws Exception {
String attrName = "PARENT";
String attrValue = "VALUE";
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new HelloController()).defaultRequest(get("/").requestAttr(attrName, attrValue)).build();
assertThat(mockMvc.perform(requestBuilder).andReturn().getRequest().getAttribute(attrName), equalTo(attrValue));
}
use of org.springframework.test.web.servlet.MockMvc in project spring-framework by spring-projects.
the class ViewResolutionTests method testContentNegotiation.
@Test
public void testContentNegotiation() throws Exception {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(Person.class);
List<View> viewList = new ArrayList<>();
viewList.add(new MappingJackson2JsonView());
viewList.add(new MarshallingView(marshaller));
ContentNegotiationManager manager = new ContentNegotiationManager(new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));
ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
cnViewResolver.setDefaultViews(viewList);
cnViewResolver.setContentNegotiationManager(manager);
cnViewResolver.afterPropertiesSet();
MockMvc mockMvc = standaloneSetup(new PersonController()).setViewResolvers(cnViewResolver, new InternalResourceViewResolver()).build();
mockMvc.perform(get("/person/Corea")).andExpect(status().isOk()).andExpect(model().size(1)).andExpect(model().attributeExists("person")).andExpect(forwardedUrl("person/show"));
mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$.person.name").value("Corea"));
mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_XML)).andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
}
use of org.springframework.test.web.servlet.MockMvc in project spring-framework by spring-projects.
the class DefaultMockMvcBuilderTests method dispatcherServletCustomizer.
/**
* See /SPR-14277
*/
@Test
public void dispatcherServletCustomizer() {
StubWebApplicationContext root = new StubWebApplicationContext(this.servletContext);
DefaultMockMvcBuilder builder = webAppContextSetup(root);
builder.addDispatcherServletCustomizer(ds -> ds.setContextId("test-id"));
builder.dispatchOptions(true);
MockMvc mvc = builder.build();
DispatcherServlet ds = (DispatcherServlet) new DirectFieldAccessor(mvc).getPropertyValue("servlet");
assertEquals("test-id", ds.getContextId());
}
Aggregations