Retrieving a thumbnail photo for an ActiveDirectory account 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() &gt; 0) {
            return searchResults.getSearchEntries()
                                .get(0)
                                .getAttributeValueBytes("thumbnailPhoto");
        }
        return null;
    }
    catch (Exception e)
    {
        return null;
    }
}
Without changing the code significantly what you need is a seam to insert a different LDAPConnection, and possibly properties. To create the seam for LDAPConnection you've got a couple of different options


Create a class that extends from the current to and overrides getAdConnection()
Constructor inject a LDAPConnectionFactory to the object and use that to replace getAdConnection()


Once you've done one of these you can inject your mock and make some assertions about how the search in being conducted and return a result structure.