title: How can I test my JwtAuthentication class using JUnit & Mockito?
body: <p>I can test my <code>JwtTokenAuthenticationFilter</code> class. How can I write the test cases of this class using Mockito &amp; JUnit? I can Only test this class.</p>
  
  <p>I don't understand how I can mock the class.</p>
  
  <pre class="lang-java prettyprint-override"><code>public class JwtTokenAuthenticationFilter extends  OncePerRequestFilter {
  
      private final JwtConfig jwtConfig;
  
      public JwtTokenAuthenticationFilter(JwtConfig jwtConfig) {
          this.jwtConfig = jwtConfig;
      }
  
      @Override
      protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
              throws ServletException, IOException {
          System.out.println("Code is reachable");
          // 1. get the authentication header. Tokens are supposed to be passed in the authentication header
          String header = request.getHeader(jwtConfig.getHeader());
          // 2. validate the header and check the prefix
          if (header == null || !header.startsWith(jwtConfig.getPrefix())) {
              chain.doFilter(request, response);
              return;// If not valid, go to the next filter.
          }
          // If there is no token provided and hence the user won't be authenticated. 
          // It's Ok. Maybe the user accessing a public path or asking for a token.
  
          // All secured paths that needs a token are already defined and secured in config class.
          // And If user tried to access without access token, then he won't be authenticated and an exception will be thrown.
  
          // 3. Get the token
          String token = header.replace("Bearer","");
  
          try {    // exceptions might be thrown in creating the claims if for example the token is expired
  
              // 4. Validate the token
              Claims claims = Jwts.parser()
                      .setSigningKey(jwtConfig.getSecret().getBytes())
                      .parseClaimsJws(token)
                      .getBody();
  
              String username = claims.getSubject();
  
              if (username != null) {
                  @SuppressWarnings("unchecked")
                  List&lt;String&gt; authorities = (List&lt;String&gt;) claims.get(ApplicationConstant.tokenAuthorities);
                  List&lt;GrantedAuthority&gt; grantAuthorities = new ArrayList&lt;GrantedAuthority&gt;();
  
                  // 5. Create auth object
                  // UsernamePasswordAuthenticationToken:A built-in object, used by spring to represent the current authenticated / being authenticated user.
                  // It needs a list of authorities, which has type of GrantedAuthority interface, where SimpleGrantedAuthority is an implementation of that interface
  
                  for (String authName : authorities) {
                      grantAuthorities.add(new SimpleGrantedAuthority(authName));
                  }
                  UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
                          username, null, grantAuthorities);
                  // 6. Authenticate the user
                  // Now, user is authenticated
                  SecurityContextHolder.getContext().setAuthentication(auth);
              }
          } catch (Exception e) {
              // In case of failure. Make sure it's clear; so guarantee user won't be authenticated
              SecurityContextHolder.clearContext();
          }
  
          // go to the next filter in the filter chain
          chain.doFilter(request, response);
      }
  }
  </code></pre>