1

I'm very new to Junit. I'm writing junit for a interceptor.. It contains SessionMap in that.. while calling the interceptor from the test class I'm getting Null pointer exception at Session Map. Below is my interceptor..

public String intercept(ActionInvocation actionInv) throws Exception {
    ActionContext context = actionInv.getInvocationContext();
    final HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
    String callerAppName = request.getParameter(CustomerPortalConstants.CALLER);
    if(callerAppName == null){
        //caller name is passed in header in case of OnePortal service request
        callerAppName = request.getHeader(CustomerPortalConstants.CALLER);
    }
    SessionMap<String,Object> sessionMap = ((SessionMap<String,Object>)ActionContext.getContext().getSession());
    @SuppressWarnings("unchecked")
    Map<String,AccountBean> accountsMap = (Map<String,AccountBean>)sessionMap.get(CustomerPortalConstants.ACCOUNTSMAP);;
     if(accountsMap == null) {
        accountsMap = new HashMap<String, AccountBean>();
        sessionMap.put(CustomerPortalConstants.ACCOUNTSMAP, accountsMap);
    }

Im getting error at this location

((SessionMap)ActionContext.getContext().getSession());

This is my Test class..

public class MultiAccountInterceptorTest extends StrutsTestCase implements SessionAware  {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private Map<String,AccountBean> accountsMap=new HashMap<String, AccountBean>();


    Map<String, Object> sessionMap;


    private String callerAppName="LMP";

    private final HttpServletRequest mockHttpReq = createMock(HttpServletRequest.class);

    MultiAccountInterceptor interceptor = new MultiAccountInterceptor();
    @SuppressWarnings("unchecked")
    @Before
    public void setUp() throws Exception {
         sessionMap = new HashMap<String, Object>();

    }

    @SuppressWarnings("unchecked")
    @Test
    public void testIntercept() throws Exception
    {
        MultiAccountInterceptor mockInterceptor = PowerMock.createNicePartialMockForAllMethodsExcept(MultiAccountInterceptor.class, "intercept");
        final ActionInvocation mockInvocation = createMock(ActionInvocation.class);
        final ActionContext mockContext = createMock(ActionContext.class);
        expect(mockInvocation.getInvocationContext()).andReturn(mockContext);
        System.out.println(mockContext);
        expect( (HttpServletRequest)mockContext.get(ServletActionContext.HTTP_REQUEST)).andReturn(mockHttpReq);
        System.out.println(mockHttpReq);
        expect(mockHttpReq.getParameter(CustomerPortalConstants.CALLER)).andReturn(callerAppName);
        System.out.println("Caller app name is"+ callerAppName);
        System.out.println(sessionMap);
        sessionMap.put(CustomerPortalConstants.ACCOUNTSMAP, accountsMap);
        System.out.println(sessionMap);
        replayAll();
     mockInterceptor.intercept(mockInvocation);

    }
    @Override
    public void setSession(Map<String, Object> sessionMap) {
        this.sessionMap=sessionMap;


    }
}

Can anyone provide me a solution for this problem.. Thanks in advance..

Shog9
  • 156,901
  • 35
  • 231
  • 235
Anu
  • 11
  • 1
  • possible duplicate of [Struts2.3.12 junit4 testcase request & session are null](http://stackoverflow.com/questions/19649952/struts2-3-12-junit4-testcase-request-session-are-null) – Aleksandr M Sep 30 '14 at 09:52
  • I tried using ActionProxy.. ActionProxy proxy=getActionProxy("/action"); it is pointing null pointer exception in the above line.. – Anu Sep 30 '14 at 10:28
  • The point is: you need to create and set session map. If you are mocking action context then you should mock session map also. – Aleksandr M Sep 30 '14 at 10:41
  • Can u please provide me any sample program that uses SessionMap. Thanks in Advance – Anu Sep 30 '14 at 11:30
  • What do you mean? You've created a action context now set session map to it. – Aleksandr M Sep 30 '14 at 11:35
  • Okay.. Thank you so much for your response.. – Anu Sep 30 '14 at 11:48
  • @AleksandrM: I guess those comments, together with a oneliner of code, should be the accepted answer... – Andrea Ligios Sep 30 '14 at 14:51
  • @AndreaLigios: Well this is pretty much duplicate, so upvotes can go into the accepted answer of that question. :D – Aleksandr M Sep 30 '14 at 14:57
  • 1
    @AleksandrM He has 6 rep. He can't upvote. But he can accept. And BTW, answering with a short answer (and with links to other answers, that are more "persistent" than comments) will help future visitors because a) sometimes duplicate questions have a nicer title, and they gather much more visits than the original one and b) a lot of people skim the comments, and watch directly if the question has an answer: if nope, they close it and look somewhere else. Other than that, pretty much everything has been asked on SO, and I guess less than 1% of the daily answers are *completely* original :> – Andrea Ligios Sep 30 '14 at 15:04
  • @AleksandrM btw I've just upvoted the original answer :D – Andrea Ligios Sep 30 '14 at 15:06
  • @AnushaAnantharaman: BTW you can answer your own question. See Andrea comment why. – Aleksandr M Sep 30 '14 at 18:36

0 Answers0