[英]ActionContext.getContext().getSession() returns null
[英]Mocking ActionContext.getContext().getSession() returns null
我正在嘗試為以下方法編寫 jUnit 測試用例。
public class MyClass {
public static Map<String, Object> getSession() {
Map<String, Object> session = ActionContext.getContext().getSession();
return session;
}
}
我關注了這個問題和這個問題並試圖模擬ActionContext
。 但是會話仍然是null
。
public class TestClass {
private HttpServletRequest request;
private HttpSession session;
@Before
public void setUp() {
// mock the session
session = mock(HttpSession.class);
// mock the request
request = mock(HttpServletRequest);
when(request.getSession()).thenReturn(session);
// set the context
Map<String, Object> contextMap = new HashMap<String, Object>();
contextMap.put(StrutsStatics.HTTP_REQUEST, request);
ActionContext.setContext(new ActionContext(contextMap));
}
@After
public void destroyTests() {
ActionContext.setContext(null);
}
@Test
public void testGetSession() {
Map<String, Object> session =MyClass.getSession();
//session is null here
}
}
我在這里做錯了什么嗎?
將以下代碼添加到上下文映射中,因為創建的是空上下文,您應該將會話設置為操作上下文。
contextMap.put(ActionContext.SESSION, new SessionMap(request));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.