如何为包含会话映射的方法编写junit测试用例

时间:2014-09-30 09:39:11

标签: junit struts2

我对Junit很新。 我正在为拦截器编写junit ..它包含SessionMap ..在从测试类调用拦截器时,我在Session Map中得到Null指针异常。 下面是我的拦截器..

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);
    }

我在这个位置得到错误

  

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

这是我的测试课..

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;


    }
}

任何人都可以为我提供这个问题的解决方案.. 提前谢谢..

0 个答案:

没有答案
相关问题