EasyMock - HttpSession设置会话 - 方法通过请求传递

时间:2013-11-21 19:51:05

标签: junit easymock httpsession

我想知道请求传递给方法并拉动HttpSession吗?

来自JUnit:

@Test
public void testSessionPass(){
    HttpServletRequest request = createMock(HttpServletRequest.class);
    HttpSession session = createMock(HttpSession.class);
    expect(session.getAttribute("testAttribute")).andReturn("testValue").anyTimes();
    replay(request);
    replay(session);

    CAction cAction = new CAction();
    cAction.test(request);


}

在CAction中:

 public void test (HttpServletRequest request){
        HttpSession session = request.getSession();
        if(session.getAttribute("testAttribute")!=null){
             System.out.println((String)session.getAttribute("testAttribute"));
        }
 }

更新

为什么我从HttpSession session = request.getSession()行传递的请求值中丢失会话; ??

1 个答案:

答案 0 :(得分:3)

你没有嘲笑对getSession()

的号召

在致电replay()

之前添加此行
expect(request.getSession()).andReturn(session);