### Question:
CaptchaAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(final Authentication authentication) throws AuthenticationException { try { captchaChecker.checkCaptcha(); } catch (CaptchaException captchaException) { authentication.setAuthenticated(false); final FailedCaptchaAuthenticationEvent event = new FailedCaptchaAuthenticationEvent( authentication, new BadCaptchaException("invalid captcha", captchaException)); eventPublisher.publishEvent(event); throw new CaptchaAuthenticationStatusException("invalid captcha", captchaException); } return null; } CaptchaAuthenticationProvider(final CaptchaChecker captchaChecker, final ApplicationEventPublisher eventPublisher); @Override Authentication authenticate(final Authentication authentication); @Override boolean supports(final Class<?> authentication);  }### Answer:
@Test void captchaInvalid() { authenticationToken = new UsernamePasswordAuthenticationToken( new Object(), new Object(), Collections.singletonList((GrantedAuthority) () -> "test")); doThrow(new CaptchaClientException("")).when(captchaChecker).checkCaptcha(); assertThrows(CaptchaAuthenticationStatusException.class, () -> captchaAuthenticationProvider.authenticate(authenticationToken)); assertThat(authenticationToken.isAuthenticated()).isFalse(); verify(eventPublisher).publishEvent(any(FailedCaptchaAuthenticationEvent.class)); }
@Test void noCaptchaOrCaptchaCorrect() { final Authentication result = captchaAuthenticationProvider.authenticate(authenticationToken); assertNull(result); }