我正在学习 Junit Mockito 以在 Intellij 中使用 Spring 3.2 测试 spring-mvc 控制器。我的控制器是
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String initUserSearchForm(ModelMap modelMap) {
User user = new User();
modelMap.addAttribute("User", user);
return "linkedInUser";
}
@RequestMapping(value = "/byName", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public
@ResponseBody
String getUserByName(HttpServletRequest request,@ModelAttribute("userClientObject") UserClient userClient) {
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
return getUserByName(userClient, firstName, lastName);
}
我所做的是我有一种表格可以按名称搜索用户。UserClient 对象是一个会话属性,我试图为我的控制器编写一个 junit 测试用例
@Test
public void testInitUserSearchForm() throws Exception {
this.liClient = client.createUserClient();
mockMvc.perform(get("/user"))
.andExpect(status().isOk())
.andExpect(view().name("user"))
.andExpect(forwardedUrl("/WEB-INF/pages/user.jsp"));
}
@Test
public void testGeUserByName() throws Exception {
String firstName = "Wills";
String lastName = "Smith";
mockMvc.perform(get("/user-byName"))
.andExpect(status().isOk());
}
如何测试我的getUserByName
方法以及如何添加会话属性?请任何人都可以帮助我编写测试用例,并对该方法进行可能的测试。提前致谢