January 28, 2011

Use Eclipse fragment plug-ins for your unit tests

Photo by b0jangles

When writing unit tests you need to be able to test all classes, including classes within internal packages (white-box testing). Of course the test classes can be placed in the same plug-in, but typically you don't want to have the test classes deployed with a release.

A fragment plug-in is typically used for platform specific content or localized resource files, but it turns out that they are very useful for testing purposes. The fragment adds all its classes to its host plug-in so that they are visible by the host classloader at run-time. You can not add dependencies directly to fragment plug-ins, but if you need to export packages from fragments it is possible. In order to see the contributed packages during development you should add the "Eclipse-ExtensibleAPI: true" to the host's MANIFEST.MF file.

Fragment plug-ins can also define and use extension points. This makes it possible to contribute tests from the fragment to a suite of tests by using an extension point.

More information on fragments and other OSGi topics can be found in the excellent book: OSGi and Equinox: Creating Highly Modular Java™ Systems