How to test your OpenDJ plugin
A problem you might face while extending the OpenDJ functionality with a plugin is to develop proper unit tests. OpenDJ comes with a set of tools to facilitate the testing, but since they are tightly integrated within the build framework, you might find it difficult to execute your unit tests from outside of the framework. This article will try to give you short guidelines on how to integrate and execute your tests.
OpenDJ inherited it’s framework from the OpenDS days which means the old documentation still provides valid guidelines and tips on how to get started with your own unit tests. It would help you get familiar with the utility classes you can use to make your test execution easier. The most relevant documents are:
Before you start working on your tests, you have to check out the OpenDJ source file tree. Say, you are developing a plugin for the 2.4 branch, so you would need the latest stable release:
mkdir opendj-dev cd opendj-dev svn co https://svn.forgerock.org/opendj/branches/b2.4/ cd b2.4
Now, your tests should go to: ‘tests/unit-testing/src/server/<package>/<unit test Java class>’ where:
- <package> is the package which your plugin belongs to, and
- <unit test Java class> is the Java class that implements the unit test(s).
For example, let’s assume we have a plugin called ‘example plugin’ and we made put it in the ‘com.example.plugins’ package (‘com.example.pluginsExamplePlugin). The path would be, say: ‘tests/unit-testing/src/server/com/example/plugins/ExamplePluginTestCase.java’.
Given that your plugin is built separately from the OpenDJ sources, and that the execution of the unit test requires OpenDJ server to be running, you have to make your plugin available to the built-in test instance (and not just the plugin, but also all the libraries you plugin depends on, schema files, etc.). For that to work you need to do the following:
- create a directory: ‘lib/extensions’;
- copy your plugin JAR (and the dependencies) to the following locations: ‘lib’ and ‘lib/extensions’;
- copy your schema file to: ‘resources/schema’.
You should be ready to execute your tests using:
./build.sh test -Dtest.classes=<your unit test>
If we take our ‘example plugin’, you would do the following:
# let’s assume you are located in opendj-dev/b2.4
cp <path to>/example-plugin.jar lib
cp <path to>/example-plugin.jar lib/extensions
cp <path to>/99-example-plugin.ldif resources/schema
./build.sh test -Dtest.classes=com.example.plugins.ExamplePluginTestCase
Testing and Maven
As previously mentioned, the testing framework of OpenDJ is very tightly integrated with the build process and makes it unavailable to the other frameworks and tools such as Maven. If you have chosen Maven to develop your plugin, you will not be able to execute unit tests using OpenDJ tools and the workaround is to follow the guide above 🙂