Fork me on GitHub

Single module Pax Exam configuration

The Pax Exam setup shown in the previous example works well for multi-module Maven projects with a dedicated Maven module for OSGi integration tests. However it is often desirable to execute the tests directly in the Maven module that builds the bundle being tested. That's especially true for small projects that could be implemented as single-module Maven projects.

It is important to remember that OSGi tests only work with fully packaged artifacts. To execute them in the same module, we need to configure them as integration tests executed by maven-failsafe-plugin (rather than unit tests executed by maven-surefire-plugin. In addition to that, we need to generate a link file for the bundle built by the current module. This can be achieved by setting the useProjectArtifact option to true in the dependencySet, but this won't work with the generate-test-resources goal because it would be executed before the package phase. Instead, we use an execution of the generate-files goal bound to the pre-integration-test phase:

<plugin>
    <groupId>com.github.veithen.alta</groupId>
    <artifactId>alta-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>generate-files</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/links</outputDirectory>
                <name>%bundle.symbolicName%.link</name>
                <value>%url%</value>
                <artifactSet>
                    <dependencySet>
                        <scope>test</scope>
                        <useProjectArtifact>true</useProjectArtifact>
                    </dependencySet>
                </artifactSet>
            </configuration>
        </execution>
    </executions>
</plugin>

The directory containing the generated link files can then be included in the classpath used by maven-failsafe-plugin by setting the additionalClasspathElements parameter:

<plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <additionalClasspathElements>
                    <additionalClasspathElement>${project.build.directory}/links</additionalClasspathElement>
                </additionalClasspathElements>
            </configuration>
        </execution>
    </executions>
</plugin>

This makes it possible to reference these link files (including the link file for the bundle built by the current project) using link:classpath: URLs, in the same way as in the multi-module setup:

@Configuration
public static Option[] configuration() {
    return options(url("link:classpath:mybundle.link"), junitBundles());
}