DirectoryServerExtension Example

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

import com.buralotech.oss.ldapunit.DirectoryServerConfiguration;
import com.buralotech.oss.ldapunit.DirectoryServerExtension;
import org.junit.Rule;
import org.junit.Test;

@ExtendWith(DirectoryServerExtension.class)
@DirectoryServerConfiguration
public class Test {

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

  @Test
  public void testSomethingWithTester(final DirectoryTester tester) {
    // Unit test code that depends on LDAP directory server using the tester
  }
}

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=buralotech,dc=com
baseDNAttributes The attribute name/value pairs to use when creating the base DN.
baseDNObjectClasses The object classes to use used when creating the base DN. domain,top
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
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.
schemaFiles The location of optional custom schema files that can be used to define the schema of the directory server. 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 classpath. `default` is a special value that indicates that the default schema should be loaded.

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.