2013-08-06 199 views
0

我有一個名爲SomeFilter一個過濾器類,它實現javax.servlet.Filter編寫單元測試提供內部模擬請求。 在doFilter方法,我設置屬性「someResult」與鍵「鍵」和值「結果」。如何屬性設置爲J2EE過濾器在過濾器

我有一個名爲TestSomeFilter的單元測試類,它調用了doFilter的SomeFilter。現在的問題是,請求屬性「someResult」我在過濾器設置,不被設置。這隻發生在我是單元測試(本地)時,即請求是模擬請求。如果我在websphere應用程序服務器上(本地)部署過濾器,則屬性設置正確。此外,MockedRequestWrapper的的setAttribute沒有得到當我運行在調試模式下的單元測試調用。

的代碼如下。

我真的很感激,如果有人能夠通過提供解決方案來解決我的問題,或者至少指引我走向正確的方向。

在此先感謝!

CODE:

public class SomeFilter implements javax.servlet.Filter { 

    public void doFilter(ServletRequest req, 
        ServletResponse res, FilterChain chain) 
    throws ServletException { 

    //do some stuff 

    HttpServletRequest request = (HttpServletRequest) req; 

    Map<String,String> map = new HashMap<String,String>(); 
    map.put("key","result"); 

    request.setAttribute("someResult",map); 
    chain.doFilter(request, res); 
    } 
} 

public class TestSomeFilter { 

    @org.junit.Test 
    public void testSomeFilter { 
     HttpServletRequest mockRequest = null; 
     HttpServletResponse mockResponse = null; 
     HttpSession mockSession = null; 
     MockedRequestWrapper mrw = null; 

     org.easymock.EasyMock.replay(mockSession); 
     org.easymock.EasyMock.replay(mockRequest); 
     org.easymock.EasyMock.replay(mockResponse); 

     mrw = new MockedRequestWrapper(mockRequest); 

     filter.doFilter(mrw, mockResponse, mockFilterChain); 

     HashMap<String,String> map = (HashMap<String,String>) mrw   
             .getAttribute(("someResult"); 

     String value = map.get("key"); 

     org.junit.Assert.assertEquals(
      "The output should be result.", 
      "result", value); 
    } 

    @org.junit.Before 
    public void setUp() throws Exception { 
     mockRequest = createNiceMock(HttpServletRequest.class); 
     mockResponse = createNiceMock(HttpServletResponse.class); 
     mockSession = createNiceMock(HttpSession.class); 
    } 

    @org.junit.After 
    public void tearDown() throws Exception {} 

    public class MockedFilterChain implements FilterChain { 

     public void doFilter(ServletRequest request, 
         ServletResponse response) 
        throws ServletException {} 
    } 
} 

public class MockedRequestWrapper extends HttpServletRequestWrapper { 

    private Map<String,Object> m = null; 
    public MockedRequestWrapper(HttpServletRequest httpServletRequest) { 
      super(httpServletRequest); 
    } 

    public void setAttribute(String name, Object o){    
     m = new HashMap<String,Object>(); 
     m.put(name, o);   
    } 

    public Object getAttribute(String name){ 
     return m.get(name);//<<<< PROBLEM: This map is always null causing NPE 
    } 
} 

回答

1

我找到了解決辦法。包裝類需要這樣修改:

public class MockedRequestWrapper extends HttpServletRequestWrapper { 

    private Map<String,Object> m = null; 
    private HttpServletRequest origReq = null; 

    public MockedRequestWrapper(HttpServletRequest httpServletRequest) 
     throws IOException, ServletException { 
     super(httpServletRequest); 
     origReq = httpServletRequest; 
    } 

    @Override 
    public void setAttribute(String name, Object o){    
     m = new HashMap<String,Object>(); 
     m.put(name, o); 
     super.setAttribute(name, o); 
     origReq.setAttribute(name, o); 
    } 

    @Override 
    public Object getAttribute(String name){ 
     if(m != null){ 
      return m.get(name); 
     } else if(origReq.getAttribute(name) != null){ 
      return origReq.getAttribute(name); 
     } else { 
      return super.getAttribute(name); 
     } 
    } 
}