spring mvc MockMvc

原创
2015/05/08 12:51
阅读数 278

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


}


展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部