Spring embedded ldap server in unit tests

Kent Lai picture Kent Lai · Feb 13, 2009 · Viewed 15.9k times · Source

I am currently trying to use an embedded ldap server for unit tests.

In Spring Security, you can quickly define an embedded ldap server for testing with the tag with some sample data loaded from the specified ldif.

I will be using Spring Ldap to perform ldap operations, and thinking of testing the usual CRUD features of my User service object.

Is there, however, a way to ensure that the entries in the embedded server to be in the same consistent state (sort of like a delete all and reload the ldif entries) for each test I am running?

I thought of the following: 1) Indicate that the method dirties the context, and force a recreation of the embedded ldap server, which sounds painful as it would have to restart the server for every method 2) Create the test entries in a test organization, such that I can unbind them and simply load in the ldif file again there.

I prefer 2, but it seems like the Spring LDAP has no good helpers to load and send across the content of a ldif file.

Any suggestions on how you perform ldap testing with an embedded ldap server of spring, or of the two possible solutions I mention?

Thanks

Answer

Gary Horton picture Gary Horton · Sep 16, 2009

Doesn't Spring LDAP provide transactional control around LDAP operations? If so, why not use Spring test framework with its auto-rollback capability?

I also know of a JDBC-LDAP bridge driver that wraps an LDAP repository, presenting it as a relational database. I've used iBatis to connect to this (I've written this up at http://lokibear.blogspot.com, see articles from July). I've not yet tried applying transactional control, but the website for the driver mentions the ability to ignore transactions (which means you can also not ignore them...right?).

Like I say, I haven't tried this yet; but, if this provides transactions around LDAP, then you can again use the Spring test framework to get auto-rollback in place. I've put out a quick cheatsheet around that framework - see the September posts at my blog.

Sorry, I might be missing your goal here; but perhaps these suggestions are useful. Good luck!