What is the best way to balance the declared export of packages from a package with the requirements of unit testing?
Consider the "mybundle" package for which you want to write unit tests. The bundle source is stored in the project IDE concept. For instance:
mybundle src/java mybundle.package1 ...java bnd.bnd
By unit tests, I mean tests of individual POJOs that are not related to the wider OSGi context in which these classes can be used in a bundle. Where possible, tests should be run with the vanilla classes loaded, for example, the JUnit slider in Eclipse.
During development, here are some ways to package unit tests:
Unit tests at beam source
Here unit tests are added to the source folder in the project:
mybundle src/java mybundle.package1 ...java test/java mybundle.package1.test ...java bnd.bnd
Pay attention to the added ".test" to delimit packages and avoid package problems.
Typically, there will be a measure to ensure that test classes do not fall into the embedded JAR package.
Unit tests in a separate kit
A separate bundle is added here, the name marked as ".test", as is generally accepted.
mybundle src/java mybundle.package1 ...java mybundle.test test/java mybundle.package1.test ...java bnd.bnd
The problem is that since the classes are separated, and running unit tests may not be familiar with the OSGi environment (for example, using the Eclipse JUnit runner), you will need to decorate the run-time class path for the JUnit runner.
Unit tests in fragment
(Thanks @HollyCummins). This creates a separate fragment of the package:
mybundle.fragment test/java mybundle.package1.test ...java bnd.bnd
The snippet declares "mybundle" as its host, which allows it to share classes in "mybundle" without the need to export packages.
The disadvantage of this is that since fragment loading is an OSGi concept, you need to run the OSGi container or decorate the classpath.
Export Issues
A problem arises when considering how packages execute Export-Package. It is considered good practice to export as few packages as possible . Nevertheless, unit testing seems to force the export of additional packages.
This is most obvious for the second option, which has a separate test suite. Tests in the test suite must import - pack the tested classes, and the test package must also Export-Package of all tested classes.
Thus, the obvious solution is to rely on unit tests in the source of connectivity, but problems soon arise in non-trivial situations. You may want to share test code, for example, you may have OSGi integration tests in a separate kit. To share the code, you will have to export the test package package, and then, of course, you will also receive the test code in the built-in package!
What is the best way to organize OSGi packages / projects for testing?