我想知道请求传递给方法并拉动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()行传递的请求值中丢失会话; ??
答案 0 :(得分:3)
你没有嘲笑对getSession()
在致电replay()
expect(request.getSession()).andReturn(session);