我是spring和junit的新手。我想使用 mockito 测试我的控制器。我使用 mock-mvc 编写了测试用例,但我的一位资深人士告诉我尝试使用 mockito。我在谷歌搜索它我不知道模拟单元测试。
@Autowired
private Client client;
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String initUserSearchForm(ModelMap modelMap) {
User user = new User();
modelMap.addAttribute("User", user);
return "user";
}
@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 client.getUserByName(userClient, firstName, lastName);
}
我的 mock-mvc 测试用例是
@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());
}
有人可以帮我吗?