TA的每日心情 | 无聊 2018-2-28 12:22 |
---|
签到天数: 4 天 连续签到: 1 天 [LV.2]测试排长
|
以下代码是登录Action中一段,使用StrutsTestCase该如何进行单元测试?
重点问题是
tusers = udao.findWhereUserIDEquals(userID);
这里的findWhereUserIDEquals()方法会在数据库中进行查找,测试时不好操作
UserLoginAction.java- public ActionForward login(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response)throws Exception{
- String userID = WebParamUtils.getParameter(request, "userID", "");
- String password = this.getParameter(request, "userpass", "");
- TbFilesystemusersDao udao = TbFilesystemusersDaoFactory.create();
- TbFilesystemusers[] tusers = null;
- TbFilesystemusers tuser = null;
- try{
- tusers = udao.findWhereUserIDEquals(userID);
- if(tusers == null || tusers.length == 0){
- request.setAttribute("message", "请您输入正确用户名及密码!");
- return mapping.findForward("logout");
- }
- tuser = tusers[0];
- request.setAttribute("userid", username);
- if( !password.equals(tuser.getUserPwd().trim())){
- request.setAttribute("message", "请您输入正确用户名及密码!");
- return mapping.findForward("logout");
- }
- Role roles[] = rdao.findWhereRoleCodeEquals(tuser.getUserrole());
- Role role = null;
- if(roles != null && roles.length > 0){
- role = roles[0];
- }
- request.getSession().setAttribute(Const.ROLE, role);
- request.getSession().setAttribute(Const.LOGIN_USER, tuser);
- }catch(Exception ex){
- ex.printStackTrace();
- request.setAttribute("message", "请您输入正确用户名及密码!");
- return mapping.findForward("logout");
- }
- return mapping.findForward("success");
- }
复制代码 有想法使用MockStrutsTestCase来测试,自己写了个MockUserLoginAction继承上面的UserLoginAction
代码如下:-
-
- public class MockUserLoginAction extends UserLoginAction{
-
- public ActionForward login(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception
- {
- String username = WebParamUtils.getParameter(request, "username", "");
- String password = this.getParameter(request, "userpass", "");
- System.out.println("username");
- System.out.println("password");
- if(username == null || username.length()== 0){
- request.setAttribute("message", "请您输入正确用户名及密码!");
- return mapping.findForward("logout");
- }
- if(username!="8000"||password!="123456")
- {
- request.setAttribute("message", "请您输入正确用户名及密码!");
- return mapping.findForward("logout");
- }
- request.setAttribute("userid", username);
- return mapping.findForward("success");
- }
- }
复制代码 测试类代码如下:-
-
- public void testLoginSuccess() throws Exception
- {
- STCRequestProcessor.addMockAction("com.zzy.record.action.user.UserLoginAction","com.zzy.record.action.user.MockUserLoginAction");
- setRequestPathInfo("/loginAction");
- addRequestParameter("method","login");
- addRequestParameter("username","8000");
- addRequestParameter("userpass","123456");
- actionPerform();
- //verifyForward("success");
- assertEquals("8000",this.getMockRequest().getAttribute("userid"));
- }
-
复制代码 但运行出错
错误信息如下:
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.zzy.record.jdbc.ResourceManager.getConnection(ResourceManager.java:13)
at com.zzy.record.jdbc.TbFilesystemusersDaoImpl.findByDynamicSelect(TbFilesystemusersDaoImpl.java:1029)
... 34 more
有没有其它办法可对其进行单元测试?
万分感谢!!!
[ 本帖最后由 fairylly 于 2009-2-12 10:06 编辑 ] |
|