My Struts2 Action classes use the below code to successfully access the session
ActionContext.getContext().getSession().clear();
However, when I try to use Junit to test my Action classes I get a NullPointer exception.
I have been reviewing some of the comments posted by others on StackOverflow and have been using the below code:
HttpServletRequest request;
HttpSession session;
@Before
public void setUp() throws Exception {
request = Mockito.mock(HttpServletRequest.class);
request.setAttribute("beanList", beanList);
request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getSession()).thenReturn(session);
Map<String, Object> contextMap = new HashMap<String, Object>();
contextMap.put(StrutsStatics.HTTP_REQUEST, request);
ActionContext.setContext(new ActionContext(contextMap));
}
However, it still throws a null pointer error. The system is able to successfully find get the context, but when it tries to get the session it dies on me. I have also tries a few different ways to accomplish the same goal to no avail. Any idea what I am doing wrong?