### Question:
AccountAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(final Authentication authentication) throws AuthenticationException { final String username = authentication.getName(); final String password = authentication.getCredentials().toString(); try { final UserDetails userDetails = accountService.authenticate(username, password); return new UsernamePasswordAuthenticationToken(userDetails, null, new ArrayList<>()); } catch (final AccountNotValidatedException ex) { return null; } }  @Override Authentication authenticate(final Authentication authentication); @Override boolean supports(final Class<?> authentication);  }### Answer:
@Test public void test_authenticate_success() throws Exception { final AuthenticatedUser authenticatedUser = new AuthenticatedUser( USERNAME, PASSWORD, EMAIL_ADDRESS, new ArrayList<>()); when(accountService.authenticate(USERNAME, PASSWORD)).thenReturn(authenticatedUser); final UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) accountAuthenticationProvider .authenticate(authentication); final AuthenticatedUser result = (AuthenticatedUser) token.getPrincipal(); assertEquals(USERNAME, result.getUsername()); assertEquals(PASSWORD, result.getPassword()); assertEquals(EMAIL_ADDRESS, result.getEmailAddress()); }
@Test public void test_authenticate_fail() throws Exception { when(accountService.authenticate(USERNAME, PASSWORD)).thenThrow(new AccountNotValidatedException()); final Authentication result = accountAuthenticationProvider.authenticate(authentication); assertNull(result); }
@Test(expected = AccountAuthenticationException.class) public void test_authenticate_exception() throws Exception { when(accountService.authenticate(USERNAME, PASSWORD)).thenThrow(new AccountAuthenticationException("test")); accountAuthenticationProvider.authenticate(authentication); }