### Question:
TokenAuthenticationFilter extends AbstractAuthFilter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; String bearerToken = resolveBearerToken(request); if (StringUtils.isBlank(bearerToken)) { chain.doFilter(request, response); return; } if (authenticationIsRequired()) { try { authInterceptor.setAccessToken(bearerToken); ResponseEntity<BearerTokenTO> validateResponse = ledgersUserMgmt.validate(bearerToken); BearerTokenTO token = Optional.ofNullable(validateResponse.getBody()) .orElseThrow(() -> new RestException("Couldn't get bearer token")); fillSecurityContext(token); } catch (FeignException | RestException e) { handleAuthenticationFailure(response, e); return; } } chain.doFilter(request, response); }  @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain);  }### Answer:
@Test void doFilter() throws IOException, ServletException { SecurityContextHolder.clearContext(); when(request.getHeader("Authorization")).thenReturn("Bearer bearerToken"); when(ledgersUserMgmt.validate(anyString())).thenReturn(ResponseEntity.ok(getBearer())); filter.doFilter(request, response, chain); verify(ledgersUserMgmt, times(1)).validate(anyString()); verify(chain, times(1)).doFilter(any(), any()); }
@Test void doFilter_null_bearer() throws IOException, ServletException { SecurityContextHolder.clearContext(); when(request.getHeader("Authorization")).thenReturn(null); filter.doFilter(request, response, chain); verify(ledgersUserMgmt, times(0)).validate(anyString()); verify(chain, times(1)).doFilter(any(), any()); }
@Test void doFilter_error() throws IOException, ServletException { SecurityContextHolder.clearContext(); when(request.getHeader("Authorization")).thenReturn("Bearer bearerToken"); when(response.getOutputStream()).thenReturn(new MockHttpServletResponse().getOutputStream()); when(ledgersUserMgmt.validate(anyString())).thenThrow(FeignException.errorStatus("method", getResponse())); response.getOutputStream().println(123); filter.doFilter(request, response, chain); verify(ledgersUserMgmt, times(1)).validate(anyString()); verify(chain, times(0)).doFilter(any(), any()); }