title: How to set attributes to a mock request inside a J2EE filter which are available in a unit test written for the filter
body: <p>I have a filter class called <code>SomeFilter</code> which implements <code>javax.servlet.Filter</code>.
  In doFilter method, I set an attribute "someResult" with key "key" and value "result". </p>
  
  <p>I have a Unit Test class called <code>TestSomeFilter</code> which invokes the doFilter of <code>SomeFilter</code>. Now the problem is that the request attribute "someResult" that I set in the filter, does not get set. This only happens when I am Unit Testing (on local) i.e. the request is a mock request. If I deploy the filter on websphere application server (on local), then the attribute is set correctly. Also, the setAttribute of the <code>MockedRequestWrapper</code> does not get invoked when I run the Unit Test in debug mode.</p>
  
  <p>The code is given below. </p>
  
  <p>I would really appreciate if someone could help me out by providing a solution to my issue or at least pointing me in the right direction. </p>
  
  <p>Thanks in advance!</p>
  
  <p>CODE:</p>
  
  <pre><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&lt;String,String&gt; map = new HashMap&lt;String,String&gt;();
      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&lt;String,String&gt; map = (HashMap&lt;String,String&gt;) 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&lt;String,Object&gt; m = null;
      public MockedRequestWrapper(HttpServletRequest httpServletRequest) {
              super(httpServletRequest);
      }
  
      public void setAttribute(String name, Object o){            
          m = new HashMap&lt;String,Object&gt;();
          m.put(name, o);         
      }
  
      public Object getAttribute(String name){
          return m.get(name);//&lt;&lt;&lt;&lt; PROBLEM: This map is always null causing NPE
      }
  }
  </code></pre>