How to write integration tests against LDAP in your spring boot java application?

How to write integration tests against LDAP in your spring boot java application?
Photo by Mathias Reding / Unsplash

The answer is to use an embedded ldap server.

spring-ldap-test bring an embedded ldap server based on ApacheDS or UnboundID.

In this post, I will demo how I use UnboundID and spring-ldap-test to do testing against an embedded LDAP server.

The UnboundID LDAP SDK for Java is a fast, powerful, user-friendly, and completely free Java library for communicating with LDAP directory servers. It offers better performance, better ease of use, and more features than other Java-based LDAP APIs. It is actively being developed and enhanced by Ping Identity and is a critical component of their Directory Server and other identity management software.

Dependencies

<dependency>
    <groupId>org.springframework.ldap</groupId>
    <artifactId>spring-ldap-test</artifactId>
    <version>${version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.unboundid</groupId>
    <artifactId>unboundid-ldapsdk</artifactId>
    <version>3.1.1</version>
    <scope>test</scope>
</dependency>

Configure Embedded LDAP server

spring.ldap.embedded.base-dn=DC=com
spring.ldap.embedded.credential.username=uid=admin
spring.ldap.embedded.credential.password=secret
spring.ldap.embedded.ldif=classpath:embedded-ldap-schema.ldif
spring.ldap.embedded.validation.enabled=false

Notes: please don't specify a port for the embedded LDAP server, it will find a free port for use. Otherwise your tests might failure due to port already been bind or used. Because each spring boot test will start an embedded LDAP server.

Init LDAP data via LDIF schema

The LDAP Data Interchange Format (LDIF) is a standard plain text data interchange format for representing Lightweight Directory Access Protocol (LDAP) directory content and update requests. LDIF conveys directory content as a set of records, one record for each object (or entry). It also represents update requests, such as Add, Modify, Delete, and Rename, as a set of records, one record for each update request. LDIF was designed in the early 1990s by Tim Howes, Mark C. Smith, and Gordon Good while at the University of Michigan.
LDAP Data Interchange Format - Wikipedia

Below employee.ldif create a few entries to be pop to the LDAP server.

That is all. Then in your spring boot tests, you should have an embedded LDAP server with the above employees entries. The LdapTemplate bean will be automatically configured for you too. I will write another post to guide you how to use LdapTemplate to do pagination search against your LDAP server.

Stay tuned!

Apache Directory Studio

I strong recommend this tool to explore your LDAP server directory while you build your spring boot java application with Spring LDAP framework.

Apache Directory Studio is a complete directory tooling platform intended to be used with any LDAP server however it is particularly designed for use with ApacheDS. It is an Eclipse RCP application, composed of several Eclipse (OSGi) plugins, that can be easily upgraded with additional ones. These plugins can even run within Eclipse itself.
Welcome to Apache Directory Studio — Apache Directory

References

15. Testing
UnboundID LDAP SDK for Java
The UnboundID LDAP SDK for Java is a fast, powerful, user-friendly, and completely free Java library for communicating with LDAP directory servers. It offers better performance, better ease of use,…

Subscribe to Post, Code and Quiet Time.

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe