pax-exam: are the tests running inside of a bundle?

600 views Asked by At

I have a Maven project that builds a very simple OSGi bundle. No activator; it's only job is to deliver some shared code to an OSGi project. I want to test that I have got the dependencies all set up and embedded correctly.

So, I've added pax-exam to the situation.

I'll paste a unit test shell at the end of this. Is my @Test method in fact running inside of a bundle that is in turn depending on the bundle built in my project?

@RunWith(PaxExam.class)
@ExamReactorStrategy(PerClass.class)
public class CommonBundleTest {

    @Configuration
    public Option[] config() {

        return options(
                // this is the current project's result artifact
                mavenBundle("com.basistech.osgi", "rosette-common-java-lib"),
                junitBundles()
        );
    }

    @Test
    public void atest() {

    }
}
2

There are 2 answers

0
Achim Nierbeck On

Are the tests running inside of a bundle: yes Pax Exam creates a TinyBundle for the Unit test itself. But it doesn't add extra dependencies on any bundle declared in the config method.

If you want to make sure those packages are imported you can alter the way the TinyBundle is build.

@ProbeBuilder
public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
    // makes sure the generated Test-Bundle contains this import!
    probe.setHeader(Constants.IMPORT_PACKAGE, "*,your.extra.package");
    return probe;
}
0
Harald Wellmann On

The so-called probe bundle created by Pax Exam on the fly contains all classes from the src/test/java folder containing your test class. The probe bundle manifest has a Dynamic-ImportPackage: * header, so it is not normally required to add explicit imports by means of a probe builder.

Any bundles required by your tests must be provisioned by a configuration option in the @COnfiguration method.

If you want your test to fail immediately when a bundle does not resolve, you can set a config property:

pax.exam.osgi.unresolved.fail = true