论坛首页 入门技术论坛

请教关于interceptor的测试方法

浏览 1989 次
该帖已经被评为新手帖
作者 正文
   发表时间:2008-03-10  
我想写关于拦截器的测试代码
以下是拦截器的代码

public String intercept(ActionInvocation actioninvocation) throws Exception {

HttpServletRequest request=ServletActionContext.getRequest();
    String username=request.getParameter("username");
    String password=request.getParameter("password");
    user=userdaoimpl.findByCredentials(username ,password);
              if(user==null){
         System.out.println("user is null");
    return Action.LOGIN;
   
    }else{
    Action action=(Action)actioninvocation.getAction();
    if(action instanceof UserAware){
    ((UserAware)action).setUser(user);
    }
    }
return actioninvocation.invoke();
}
以下是测试的代码
public void setUp()throws Exception{
actioninvocation=EasyMock.createMock(ActionInvocation.class);
}
public void testInterceptor() throws Exception{

MockControl mockcontrol=MockControl.createControl(HttpServletRequest.class);
HttpServletRequest request=(HttpServletRequest)mockcontrol.getMock();
request.getParameter("username");
mockcontrol.setReturnValue("a", 1);
request.getParameter("password");
mockcontrol.setReturnValue("a", 1);
mockcontrol.replay();
InterceptorLogin interceptor=new InterceptorLogin();
         assertEquals("Login failed.", interceptor.intercept(actioninvocation));


}
请问这样以上代码写有什么不妥
测试显示结果String username=request.getParameter("username");是空值
请问request.getParameter("username");的测试环境如何搭建?
谢谢
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics