### Question:
AmbariBasicAuthenticationFilter extends BasicAuthenticationFilter implements AmbariAuthenticationFilter { @Override protected void onUnsuccessfulAuthentication(HttpServletRequest servletRequest, HttpServletResponse servletResponse, AuthenticationException authException) throws IOException { if (eventHandler != null) { AmbariAuthenticationException cause; if (authException instanceof AmbariAuthenticationException) { cause = (AmbariAuthenticationException) authException; } else { String header = servletRequest.getHeader("Authorization"); String username = null; try { username = getUsernameFromAuth(header, getCredentialsCharset(servletRequest)); } catch (Exception e) { LOG.warn("Error occurred during decoding authorization header.", e); } cause = new AmbariAuthenticationException(username, authException.getMessage(), false, authException); } eventHandler.onUnsuccessfulAuthentication(this, servletRequest, servletResponse, cause); } } AmbariBasicAuthenticationFilter(AuthenticationManager authenticationManager,
                                         AmbariEntryPoint ambariEntryPoint,
                                         AmbariAuthenticationEventHandler eventHandler); @Override boolean shouldApply(HttpServletRequest httpServletRequest); @Override boolean shouldIncrementFailureCount(); @Override void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain);  }### Answer:
@Test public void testDoFilterUnsuccessful() throws IOException, ServletException { Capture<? extends AmbariAuthenticationFilter> captureFilter = newCapture(CaptureType.ALL); HttpServletRequest request = createNiceMock(HttpServletRequest.class); HttpServletResponse response = createMock(HttpServletResponse.class); HttpSession session = createMock(HttpSession.class); FilterChain filterChain = createMock(FilterChain.class); expect(request.getHeader("Authorization")).andReturn("Basic YWRtaW46YWRtaW4=").once(); expect(request.getRemoteAddr()).andReturn("1.2.3.4").once(); expect(request.getSession(false)).andReturn(session).once(); expect(session.getId()).andReturn("sessionID").once(); expect(authenticationManager.authenticate(anyObject(Authentication.class))).andThrow(new InvalidUsernamePasswordCombinationException("user")).once(); eventHandler.beforeAttemptAuthentication(capture(captureFilter), eq(request), eq(response)); expectLastCall().once(); eventHandler.onUnsuccessfulAuthentication(capture(captureFilter), eq(request), eq(response), anyObject(AmbariAuthenticationException.class)); expectLastCall().once(); entryPoint.commence(eq(request), eq(response), anyObject(AmbariAuthenticationException.class)); expectLastCall().once(); replayAll(); AmbariAuthenticationFilter filter = new AmbariBasicAuthenticationFilter(authenticationManager, entryPoint, eventHandler); filter.doFilter(request, response, filterChain); verifyAll(); List<? extends AmbariAuthenticationFilter> capturedFilters = captureFilter.getValues(); for (AmbariAuthenticationFilter capturedFiltered : capturedFilters) { Assert.assertSame(filter, capturedFiltered); } }