title: How do i mock JWT.decode?
body: <p>I am using this library in my Spring boot application.</p>
  
  <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;com.auth0&lt;/groupId&gt;
    &lt;artifactId&gt;java-jwt&lt;/artifactId&gt;
    &lt;version&gt;3.9.0&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>How do I do my unit test for <code>DecodedJWT jwt = JWT.decode(accessToken);</code>? </p>
  
  <p>I could just pass in an actual token in but that is not the correct way to do it.</p>
  
  <p>I have this JwtAuthenticationFilter in my Spring Boot app.</p>
  
  <pre><code>@Component
  public class JwtAuthenticationFilter extends OncePerRequestFilter {
  
      @Value("${clientid}")
      private String clientid;
  
      @Autowired
      private AuthenticationService authenticationService;
  
      @Override
      protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException, InvalidRoleException {
  
          getJwtFromRequest(request, response, filterChain);
      }
  
      private void getJwtFromRequest(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
  
              String bearerToken = request.getHeader("Authorization");
  
              if (!StringUtils.hasText(bearerToken) || !bearerToken.startsWith("Bearer ")) {
                  throw new AccessTokenMissingException("No access token found in request headers.");
              }
  
              try {
                  String accessToken = bearerToken.substring(7);
  
                  // this will also throw error when unable to reach auth server
                  ResponseEntity&lt;String&gt; result = authenticationService.getUserInfo(accessToken);
  
                  // Invalid access token
                  if (!result.getStatusCode().is2xxSuccessful()) {
                      throw new InvalidAccessTokenException("Invalid access token.");
                  }
  
                  DecodedJWT jwt = JWT.decode(accessToken);
  
                  String username = jwt.getClaim("preferred_username").asString();
                  Map&lt;String, Object&gt; resources = jwt.getClaim("resource_access").asMap();
  
                  Object roles = ((Map&lt;String, Object&gt;) resources.get(clientid)).get("roles");
  
                  List&lt;String&gt; rolesList = (ArrayList&lt;String&gt;)roles;
  
                  UserInfo user = new UserInfo();
                  user.setUsername(username);
                  user.setRole(rolesList);
  
                  // Step 3: Set username to security context
                  UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
                          user.getUsername(), null, AuthUtil.getAuthRole(user.getRole()));
  
                  SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
  
              } catch (HttpClientErrorException.Unauthorized | JWTDecodeException e) {
                  throw new InvalidAccessTokenException("Invalid access token.");
              }
  
          filterChain.doFilter(request, response);
      }
  }
  </code></pre>