### Question:
LdapConfigurationStoreBuilder { public LdapConfigurationStoreBuilder addUsingSearch(final String searchBase, final String filter, final String keyAttribute, final String... tags) { SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); try { final NamingEnumeration<SearchResult> searchResultNamingEnumeration = dirContext.search(createName( searchBase), filter, searchControls); while (searchResultNamingEnumeration.hasMore()) { final SearchResult result = searchResultNamingEnumeration.next(); final Attributes attributes = result.getAttributes(); final Attribute attribute = attributes.get(keyAttribute); if (attribute == null) { throw new ConstrettoException(String.format( "The LDAP object \"%1$s\" has no attribute value for attribute \"%2$s\"", result.getName(), keyAttribute)); } final String key = attribute.get().toString(); ldapConfigurationStore = new LdapConfigurationStore(ldapConfigurationStore, key, attributes, tags); } } catch (NamingException e) { throw new ConstrettoException( String.format("An error occurred while searching LDAP using searchBase \"%1$s\" and filter \"%2$s\"", searchBase, filter), e); } return this; } private  LdapConfigurationStoreBuilder(final DirContext dirContext); static LdapConfigurationStoreBuilder usingDirContext(final DirContext dirContext); LdapConfigurationStoreBuilder addDsnWithKey(final String key,
                                                       final String distinguishedName,
                                                       String... tags); LdapConfigurationStoreBuilder addDsn(final String distinguishedName, final String... tags); LdapConfigurationStoreBuilder addUsingSearch(final String searchBase,
                                                        final String filter,
                                                        final String keyAttribute,
                                                        final String... tags); LdapConfigurationStore done(); static final String NULL_ARGUMENT; }### Answer:
@Test(expected = ConstrettoException.class) public void testAddUsingSearchKeyAttributeNotFound() throws Exception { final String filter = "(objectClass=person)"; when(dirContext.search(any(Name.class), eq(filter), any(SearchControls.class))).thenReturn(searchResults); when(searchResults.hasMore()).thenReturn(true); when(searchResults.next()).thenReturn(searchResult); when(searchResult.getAttributes()).thenReturn(attributes); final String noneExistingKey = "NoneExistingKey"; when(attributes.get(eq(noneExistingKey))).thenReturn(null); when(searchResult.getName()).thenReturn("cn=me,dc=here,dc=com"); try { ldapConfigurationStoreBuilder.addUsingSearch("dc=here,dc=com", filter, noneExistingKey); } finally { verify(dirContext).search(any(Name.class), eq(filter), any(SearchControls.class)); verify(searchResults).hasMore(); verify(searchResults).next(); verify(searchResult).getAttributes(); verify(attributes).get(eq(noneExistingKey)); verify(searchResult).getName(); } }
@Test(expected = ConstrettoException.class) public void testAddUsingSearchKeyFailes() throws Exception { final String filter = "(objectClass=person)"; when(dirContext.search(any(Name.class), eq(filter), any(SearchControls.class))).thenThrow(new NamingException()); final String noneExistingKey = "NoneExistingKey"; try { ldapConfigurationStoreBuilder.addUsingSearch("dc=here,dc=com", filter, noneExistingKey); } finally { verify(dirContext).search(any(Name.class), eq(filter), any(SearchControls.class)); } }