DirectoryServerRule Example

DirectoryServerRule is used launch an embedded LDAP directory server so that the unit tests do not have to be dependent on an external LDAP directory server with unpredictable state.

import com.btmatthews.ldapunit.DirectoryServerConfiguration;
import com.btmatthews.ldapunit.DirectoryServerRule;
import org.junit.Rule;
import org.junit.Test;

@DirectoryServerConfiguration
public class Test {

  @Rule
  public DirectoryServerRule directoryServerRule = new DirectoryServerRule();

  @Test
  public void testSomething() {
    // Unit test code that depends on LDAP directory server
  }
}

The DirectoryServerConfiguration annotation is used to provide the configuration parameters for the embedded LDAP directory server. The following parameters can be configured:

Name Description Default
port The TCP port that the LDAP directory server will be configured to listen on. 10389
baseDN The DN that will be configured as the root of the LDAP directory. dc=btmatthews,dc=com
authDN The DN that will be configured as the administrator account identifier. uid=admin,ou=system
password The password that will be configured as the authentication credentials for the administrator account. secret
ldifFile The location of an optional LDIF file that can be used to seed the LDAP directory with an initial data set. The file may be located on the file system or the classpath. The classpath is checked first and then falls back to the file system if it was not found on the class path.
ldifFiles The locations of an optional LDIF files that can be used to seed the LDAP directory with an initial data set. The files may be located on the file system or the classpath. The classpath is checked first and then falls back to the file system if it was not found on the class path.

The following methods can be used to make assertions about or verify the contents of the LDAP directory:

  • assertDNExists(String dn) - asserts that an entry exists in the LDAP directory with the distinguished name of dn.
  • verifyDNExists(String dn) - tests to see if an entry exists in the LDAP directory with the distinguished name of dn.
  • assertDNIsA(String dn, String objectclass) - asserts that an entry exists in the LDAP directory with the distinguished name of dn and that it is of type objectclass.
  • verifyDNIsA(String dn, String objectclass) - tests to see if an entry exists in the LDAP directory with the distinguished name of dn and check that it is of type objectclass.
  • assertDNHasAttribute(String dn, String attributeName) - asserts that an entry exists in the LDAP directory with the distinguished name of dn and that it has an attribute named attributeName.
  • verifyDNHasAttribute(String dn, String attributeName) - tests to see if an entry exists in the LDAP directory with the distinguished name of dn and check that it has an attribute named attributeName.
  • assertDNHasAttributeValue(String dn, String attributeName, String... attributeValue) - assert that an entry exists in the LDAP directory with the distinguished name of dn and that it has an attribute named attributeName with value(s) attributeValues.
  • verifyDNHasAttributeValue(String dn, String attributeName, String... attributeValue) - tests to see if an entry exists in the LDAP directory with the distinguished name of dn and check that it has an attribute named attributeName with value(s) attributeValues.