@Test
public void testAuthenticateBadPassword() throws Exception {

  String basePathString = "dc=apache,dc=org";
  String ldapUserRelativeDNString = String.format("uid=%s,ou=people,ou=dev", "ldapUsername");
  LdapName ldapUserRelativeDN = new LdapName(ldapUserRelativeDNString);
  String ldapUserDNString = String.format("%s,%s", ldapUserRelativeDNString, basePathString);
  LdapName basePath = LdapUtils.newLdapName(basePathString);
  LdapContextSource ldapCtxSource = createMock(LdapContextSource.class);
  expect(ldapCtxSource.getBaseLdapName())
      .andReturn(basePath)
      .atLeastOnce();
  expect(ldapCtxSource.getContext(ldapUserDNString, "password"))
      .andThrow(new org.springframework.ldap.AuthenticationException(null))
      .once();
  DirContextOperations searchedUserContext = createMock(DirContextOperations.class);
  expect(searchedUserContext.getDn())
      .andReturn(ldapUserRelativeDN)
      .atLeastOnce();
  FilterBasedLdapUserSearch userSearch = createMock(FilterBasedLdapUserSearch.class);
  expect(userSearch.searchForUser(anyString())).andReturn(searchedUserContext).once();
  replayAll();
  Configuration configuration = new Configuration();
  AmbariLdapBindAuthenticator bindAuthenticator = new AmbariLdapBindAuthenticator(ldapCtxSource, configuration);
  bindAuthenticator.setUserSearch(userSearch);
  try {
    bindAuthenticator.authenticate(new UsernamePasswordAuthenticationToken("username", "password"));
    fail("Expected thrown exception: org.springframework.security.authentication.BadCredentialsException");
  } catch (org.springframework.security.authentication.BadCredentialsException e) {
    // expected
  } catch (Throwable t) {
    fail("Expected thrown exception: org.springframework.security.authentication.BadCredentialsException\nEncountered thrown exception " + t.getClass().getName());
  }
  verifyAll();
}