title: Unit Testing Servlet Filter with Mockito using HttpSession
body: <p>I search for a long time how to do unit test with mockito and the class HttpSession. I haven't find anything so I'm asking here. </p>
  
  <p>I have a web app in JavaEE. I implemented three filters that use a custom User class to log on the website. One filter by type of users. I have to test them with JUnit and Mockito. The only issue here, is that I don't succeeded to reproduce the behavior of the httpSession and httpServletRequest. 
  When the test is passing through the filter code, when it comes to <code>session.getAttribute("User")</code>, it return a null pointer exception. 
  Can you have a sight on this code please ? Basically, I need to emulate a session to get the "user" attribute and verify if the redirection is correct.</p>
  
  <p>Filter code:</p>
  
  <pre><code>/**
   * Default constructor.
   */
  public RestrictionFilterCaseAdmin ( )
  {
      // TODO Auto-generated constructor stub
  }
  
  
  /**
   * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
   */
  public void doFilter( ServletRequest req , ServletResponse res ,
                  FilterChain chain )
                  throws IOException , ServletException
  {
  
      HttpServletRequest request = ( HttpServletRequest ) req;
      HttpServletResponse response = ( HttpServletResponse ) res;
  
      HttpSession session = request.getSession( );
  
      /* Creating temp user */
      if ( session.getAttribute( "User" ) != null ) {
          User userTmp = new User( );
          userTmp = ( User ) session.getAttribute( "User" );
  
          /**
           * If the user object don't exist, the user is not
           * connected.
           */
          if ( userTmp.getLogin( ) == null ) {
              /* Public page redirection */
              response.sendRedirect( request.getContextPath( )
                              + "/connexion" );
          } else if ( userTmp.getStatus( ).equals( "admin" ) ) {
              /* Restricted page redirection */
              chain.doFilter( request , response );
          } else {
              // User not allowed to dislay the page, go back
              // to home.
              response.sendRedirect( request.getContextPath( )
                              + "/redirect" );
          }
      } else {
          response.sendRedirect( request.getContextPath( )
                          + "/connexion" );
      }
  }
  
  
  @ Override
  public void destroy( )
  {
      // TODO Auto-generated method stub
  
  }
  
  
  @ Override
  public void init( FilterConfig arg0 ) throws ServletException
  {
      // TODO Auto-generated method stub
  
  }
  </code></pre>
  
  <p>Test code: </p>
  
  <pre><code>@ Mock
  HttpServletRequest  httpServletRequest;
  
  @ Mock
  HttpServletResponse httpServletResponse;
  
  @ Mock
  FilterChain     filterChain;
  
  @ Mock
  User            userTest;
  
  @ Mock
  HttpSession     sessionTest;
  
  
  @ Before
  public void setUp( )
  {
      httpServletRequest = mock( HttpServletRequest.class );
      httpServletResponse = mock( HttpServletResponse.class );
      filterChain = mock( FilterChain.class );
      userTest = mock( User.class );
      sessionTest = mock( HttpSession.class );
  }
  
  
  @ Test
  public void testDoFilter( ) throws IOException , ServletException
  {
      userTest.setLogin( "test" );
      userTest.setPwd( "test" );
      userTest.setStatus( "admin" );
      sessionTest.setAttribute( "User" , userTest );
      when( sessionTest.getAttribute( "User" ) )
                      .thenReturn( userTest );
      when( httpServletRequest.getSession( true ) )
                      .thenReturn( sessionTest );
      when( httpServletRequest.getRequestURI( ) )
                      .thenReturn( "/Home.jsp" );
      RestrictionFilterCaseAdmin adminFilter = new RestrictionFilterCaseAdmin( );
      adminFilter.doFilter( httpServletRequest , httpServletResponse ,
                      filterChain );s
      verify( httpServletResponse ).sendRedirect(
                      "/restricted/admin/restrictedAdminTest.jsp" );
  }
  </code></pre>
  
  <p>Thank you very much ! 
  J.K</p>