spring mvc MockMvc

@WebAppConfiguration

@ContextConfiguration(locations = {
"classpath:spring-context.xml","classpath:spring-servlet.xml"})
public class AbstractContextControllerTests {

@Autowired
protected WebApplicationContext wac;

}



@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(defaultRollback = true)
public class UserControllerTests extends AbstractContextControllerTests {


private MockMvc mockMvc;


@Before
public void setup() throws Exception {
this.mockMvc = webAppContextSetup(this.wac).alwaysExpect(status().isOk()).build();
}


@Test
public void byParameter() throws Exception {
this.mockMvc.perform(get("/user/userRegister?userName=123&&password=123")).andExpect(
content().string("Mapped by path + method + presence of query parameter!"));
}

@Test
public void testSave() throws Exception {
 ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.put("/user/userRegister").accept(MediaType.APPLICATION_JSON).param("userName", "1000").param("password", "test"));
 MvcResult mvcResult = resultActions.andReturn();
 ModelAndView modelAndView = mvcResult.getModelAndView();
 System.out.println(modelAndView.getViewName());
 assertEquals("redirect:null", modelAndView.getViewName());
}


}


你可能感兴趣的:(spring mvc MockMvc)