Question

I'm trying to setup tests with PAX Exam as follows:

@ExamReactorStrategy(PerMethod.class)
public class AbstractTest {

    @Configuration
    public Option[] config() {
        return options(
                junitBundles(),

                /* PAX Logging */
                mavenBundle("org.ops4j.pax.logging", "pax-logging-api", "1.7.2"),
                mavenBundle("org.ops4j.pax.logging", "pax-logging-service", "1.7.2"),

                /* Apache Felix Config Admin */
                mavenBundle("org.apache.felix", "org.apache.felix.configadmin", "1.8.0"),

                /* Eclipse Gemini dependencies */
                mavenBundle().groupId("org.aopalliance").artifactId("com.springsource.org.aopalliance").versionAsInProject(),
                mavenBundle().groupId("org.springframework").artifactId("org.springframework.aop").versionAsInProject(),
                mavenBundle().groupId("org.springframework").artifactId("org.springframework.beans").versionAsInProject(),
                mavenBundle().groupId("org.springframework").artifactId("org.springframework.context").versionAsInProject(),
                mavenBundle().groupId("org.springframework").artifactId("org.springframework.core").versionAsInProject(),

                /* Eclipse Gemini */
                mavenBundle("org.eclipse.gemini.blueprint", "gemini-blueprint-core", GEMINI_VERSION),
                mavenBundle("org.eclipse.gemini.blueprint", "gemini-blueprint-extender", GEMINI_VERSION),
                mavenBundle("org.eclipse.gemini.blueprint", "gemini-blueprint-io", GEMINI_VERSION),

                /* Other bundles */;
    }

    @Before
    public void setUp() throws Exception {
        ....
    }

}


@RunWith(PaxExam.class)
public class MyTest extends AbstractTest {

    @Inject
    private MyObject myObject;

    @Test
    public void testOne() {
        ...
    }

}

For some reason, the method annotated with @Before is not called.

Thanks, Mickael


EDIT: The PAX EXAM dependencies I use are:

<dependency>
    <groupId>org.ops4j.pax.exam</groupId>
    <artifactId>pax-exam-container-native</artifactId>
    <version>${pax.exam.version}</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.ops4j.pax.exam</groupId>
    <artifactId>pax-exam-junit4</artifactId>
    <version>${pax.exam.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.ops4j.pax.exam</groupId>
    <artifactId>pax-exam-link-mvn</artifactId>
    <version>${pax.exam.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.ops4j.pax.url</groupId>
    <artifactId>pax-url-aether</artifactId>
    <version>${url.version}</version>
    <scope>test</scope>
</dependency>

I use PAX EXAM version 3.4.0.

Was it helpful?

Solution

I've set up a test project and it worked fine.

I finally found what is causing the issue. It seems that the execution of the tests is stopped during the configuration.

I use Eclipse Gemini 2.0.M02 and I use placeholders in my application contexts. For example, I need the datasource URL to be set as property. I wanted to use a @Before method to setup a configuration for a PID using the Configuration Admin service.

Since the application contexts are loaded during the configuration and that at this point the PID is not configured (since the @Before method is not called yet), I get an error from Spring which causes PAX EXAM to abort execution of current test.

Therefore, I can't use the Configuration Admin Service from the @Before method in my case.

The solution is to use the pax-exam-cm module which allows interacting with the Configuration Admin Service inside the configuration method as follows:

@Configuration
public static Option[] config() {
    return options(
            junitBundles(),

            /* PAX Logging */
            mavenBundle("org.ops4j.pax.logging", "pax-logging-api", "1.7.2"),
            mavenBundle("org.ops4j.pax.logging", "pax-logging-service", "1.7.2"),

            /* Apache Felix Config Admin */
            mavenBundle("org.apache.felix", "org.apache.felix.configadmin", "1.8.0"),

            ConfigurationAdminOptions.newConfiguration("my.pid")
                .put("prop1", "value1")
                .put("prop2", "value2")
                .asOption(),

            /* Eclipse Gemini + dependencies */
            mavenBundle().groupId("org.aopalliance").artifactId("com.springsource.org.aopalliance").versionAsInProject(),
            mavenBundle().groupId("org.springframework").artifactId("org.springframework.aop").versionAsInProject(),
            mavenBundle().groupId("org.springframework").artifactId("org.springframework.beans").versionAsInProject(),
            mavenBundle().groupId("org.springframework").artifactId("org.springframework.context").versionAsInProject(),
            mavenBundle().groupId("org.springframework").artifactId("org.springframework.core").versionAsInProject(),
            mavenBundle("org.eclipse.gemini.blueprint", "gemini-blueprint-core", GEMINI_VERSION),
            mavenBundle("org.eclipse.gemini.blueprint", "gemini-blueprint-extender", GEMINI_VERSION),
            mavenBundle("org.eclipse.gemini.blueprint", "gemini-blueprint-io", GEMINI_VERSION),

            /* Other bundles */
            );
}

You also need to add the pax-exam-cm dependency to your POM (if you use Maven):

<dependency>
    <groupId>org.ops4j.pax.exam</groupId>
    <artifactId>pax-exam-cm</artifactId>
    <version>3.4.0</version>
    <scope>test</scope>
</dependency>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top