刚来第三天,领导先让在spring下用junit测试,现在让测试session,不知道怎么搞,求大神指导。。 |
|
20分 |
引入Spring-test包,然后:
MockHttpSession session = new MockHttpSession(); |
具体怎么用啊。。
|
|
20分 |
帮到底吧,举个例子,假如要测试如下方法: public class HomeAction{ public String index(HttpServletRequest request, HttpServletResponse response){ HttpSession session = request.getSession(); //如果用户登录了就进入个人空间,否则就进入主页 if(session.getAttribute("login_user") != null ){ return "myhome"; } return "index"; } } //认真看测试方法 public class HomeActionTest{ public void testIndex(){ MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.getSession().setAttribute("login_user", new User("howsun")); HomeAction action = new HomeAction(); String returnValue = action.index(request, response); Asserts.isTrue("myhome".equals(returnValue));//断言测试结果必定是"myhome" } } PS:新工作环境下不要太多压力,灵活多变。加油! |