Menu

InMemoryDirectoryServer used for JUnit testing?

2015-09-14
2015-09-15
  • Jason "JRSofty" Reed

    Hello,

    I was wondering if it is possible to use the InMemoryDirectoryServer interface for creating a Mock of a LDAP Server for JUnit testing. I did find the examples in your JavaDoc and that seems ok until I try to import the base LDIF. It seems I need to be able to provide it with the schema chagnes, but I don't see how to do that. Is there a way I can set the schema for the InMemoryDirectorySever?

    Regards,
    Jason Reed

     

    Last edit: Jason "JRSofty" Reed 2015-09-14
  • Neil Wilson

    Neil Wilson - 2015-09-14

    Absolutely. Unit testing is one of the key use cases for the in-memory directory server. Although I've only used it in TestNG unit tests, it should work just fine with JUnit. It's used extensively in the unit tests for the LDAP SDK itself (along with other directory servers).

    As for the schema, you have a couple of options. The best option is to configure the in-memory directory server with the same schema as the production server you want to simulate. The InMemoryDirectoryServerConfig.setSchema method is used to specify the schema to use for the server, and there are Schema.getSchema methods that allow you to load schema information from LDIF files that you provide as part of your test framework. There's also a Schema.getDefaultStandardSchema method that populates the server with a set of common schema elements defined in a number of RFCs and IETF drafts. If you're using a directory server that doesn't store schema in LDIF files, then you can retrieve the schema from the directory server you want to emulate (using LDAPConnection.getSchema or Schema.getSchema) and then use the getSchemaEntry to get an entry containing the schema definition and then store that entry as LDIF.

    The other option is to provide a value of null to the InMemoryDirectoryServerConfig.setSchema method. This will essentially disable schema checking for the in-memory directory server, and it will accept entries with any attributes and object classes that you provide. I wouldn't really recommend this option, though, because it makes it too easy for application bugs to slip through as a result of changes that don't conform to the schema requirements.

    And on a marginally-related note, if you're using the LDAP SDK in unit tests, then the com.unboundid.util.LDAPTestUtils class provides a number of convenience methods that are useful for performing tests to ensure that entries exist or are missing, that entries have the expected content, for ensuring that operations yield the expected results, etc.

     
  • Jason "JRSofty" Reed

    Ah that's really useful. Thanks.

     

Log in to post a comment.