I have the following method and I would like to understand how it could be reorganized to be more unit-testable.
I think the section about binding to Active Directory could go to its own method, making this method more about either returning bytes or not.
public byte[] getThumbnail(String username) {
SimpleBindRequest formInputBindRequest =
new SimpleBindRequest(MASTER_USER_DN, MASTER_PASSWORD);
try {
LDAPConnection adConnection = getAdConnection();
adConnection.bind(formInputBindRequest);
SearchResult searchResults = adConnection.search(properties.getSearchBase(),
SearchScope.SUB, "(sAMAccountName=" + username + ")", "thumbnailPhoto");
if (searchResults.getEntryCount() > 0) {
return searchResults.getSearchEntries()
.get(0)
.getAttributeValueBytes("thumbnailPhoto");
}
return null;
}
catch (Exception e)
{
return null;
}
}
LDAPConnection
and the like. That then leads you down the mocking rabbit hole, where you are testing your mocks, not your production code. I'd favour using some sort of lightweight LDAP service (Microsoft's ADLDS, or Apache Directory are two possibilities) that can then be created and torn down as part of a set of integration tests. That way, you are actually testing your use of LDAP functionality, rather than those mocks. \$\endgroup\$