Spring embedded ldap server in unit tests

I am currently trying to use the built-in ldap server for unit tests.

In Spring Security, you can quickly define the built-in ldap server for testing with a tag with some data samples loaded from the specified ldif.

I will use Spring Ldap to perform ldap operations and think about checking the regular CRUD functions of my user service object.

Is there a way to guarantee that the entries in the embedded server will be in the same state (sort of like deleting everything and reloading the ldif entries) for every test that I run?

I thought about the following: 1) Indicate that the method distorts the context and makes the built-in ldap server recreate, which seems painful because it had to restart the server for each method 2) Create test records in the test organization so that I can untie them and just load there is an ldif file.

I prefer 2, but it looks like Spring LDAP does not have good helpers to download and send the contents of the ldif file.

Any suggestions on how you do ldap testing with the built-in ldap spring server or two possible solutions that I mention?

thanks

+5
source share
4 answers

, LDAP, Mock out LDAP Mock, , Unit .

LDAP, . , , LDAP.

+4

Spring LDAP LDAP? , Spring ?

JDBC-LDAP, LDAP, . iBatis ( http://lokibear.blogspot.com, . ). , - ( , ... ?).

, ; LDAP, Spring, . - - . .

, ; , , . !

+3

, LDAP Spring LDAP, Apache Directory Server. , LDIF Apache DS ( Spring, ) , , , , , unit test. , , Spring Security LDAP Apache DS LDIF.

As an alternative, you can opt out of LDIF altogether and create your own unit test shell, which checks the pre and post data conditions before running your unit tests. It will be more work, but may ultimately work better for you.

+2
source

Works great for me:

@Inject
private ApplicationContext applicationContext;

@Before
public void reloadLdapDirectory() throws NamingException, IOException{
    ApacheDSContainer apacheDSContainer = (ApacheDSContainer) applicationContext.getBean(BeanIds.EMBEDDED_APACHE_DS);
    LdapTestUtils.clearSubContexts(contextSource, DistinguishedName.EMPTY_PATH);

    ClassPathResource classPathResource = new ClassPathResource("ldap.ldif");

    File tempFile = File.createTempFile("spring_ldap_test", ".ldif");
    try {
        InputStream inputStream = classPathResource.getInputStream();
        IOUtils.copy(inputStream, new FileOutputStream(tempFile));
        LdifFileLoader fileLoader = new LdifFileLoader(apacheDSContainer.getService().getAdminSession(), tempFile.getAbsolutePath());
        fileLoader.execute();
    }
    finally {
        try {
            tempFile.delete();
        }
        catch (Exception e) {
            // Ignore this
        }
    }
}

I asked something like this and got a response from Luke Taylor: Integration tests with spring-security and ldap

+1
source

All Articles