2013-11-21 74 views
0

我想知道請求傳遞給方法並拉動HttpSession?EasyMock - 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")); 
     } 
} 

UPDATE:

爲什麼我失去從線HttpSession的會議傳遞的請求值會話= request.getSession(); ??

+0

什麼是你的問題? – dkatzel

+0

當我傳遞請求參數時,我失去了testAttribute的會話值。它實際上在HttpSession session = request.getSession()上死亡;線。 – ogottwald

回答

3

你是不是嘲諷調用getSession()

你的電話之前添加此行replay()

expect(request.getSession()).andReturn(session); 
+0

非常感謝你!你是最棒的! – ogottwald