BundleContext is null in unit tests using pax-exam

I am using pax-exam to download, activate and access osgi packages.

The following source code is my pax-exam test and it is run using pax-exam 2.3 using its own container.

package fr.xlim.ssd.vtg.osgi;

import java.net.URISyntaxException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Inject;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.Configuration;
import org.ops4j.pax.exam.junit.ExamReactorStrategy;
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import org.ops4j.pax.exam.spi.reactors.AllConfinedStagedReactorFactory;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.junit.Assert.assertNotNull;
import static org.ops4j.pax.exam.CoreOptions.bundle;
import static org.ops4j.pax.exam.CoreOptions.junitBundles;
import static org.ops4j.pax.exam.CoreOptions.options;

@RunWith(JUnit4TestRunner.class)
@ExamReactorStrategy(AllConfinedStagedReactorFactory.class)
public class BasicOSGILoaderTest {

    private Logger logger = LoggerFactory.getLogger(BasicOSGILoaderTest.class);

    @Inject
    private BundleContext bundleContext;

    @Configuration
    public Option[] config() {

    String projectRoot = null;

    try {
       projectRoot = ClassLoader.getSystemResource(".").toURI().toURL().toString();
       projectRoot = projectRoot.toString().substring(0,projectRoot.toString().length() - 27);
       logger.debug("project root: {}", projectRoot);
    } catch (MalformedURLException e) {
       logger.error("malformed URL to project root: {}", projectRoot, e);
       throw new IllegalStateException(e);
    } catch (URISyntaxException e) {
       logger.error("URI Syntax Error to project root: {}", projectRoot, e);
       throw new IllegalStateException(e);
    }

    return options(
       junitBundles(),
       logProfile(),
       bundle(projectRoot + "libs/org.eclipse.ant.core_3.2.300.v20110511.jar"),
       bundle(projectRoot + "libs/org.eclipse.core.contenttype_3.4.100.v20110423-0524.jar"),
       bundle(projectRoot + "libs/org.eclipse.core.expressions_3.4.300.v20110228.jar"),
       bundle(projectRoot + "libs/org.eclipse.core.jobs_3.5.100.v20110404.jar"),
       bundle(projectRoot + "libs/org.eclipse.core.resources_3.7.100.v20110510-0712.jar"),
       bundle(projectRoot + "libs/org.eclipse.core.runtime_3.7.0.v20110110.jar"),
       bundle(projectRoot + "libs/org.eclipse.equinox.app_1.3.100.v20110321.jar"),
       bundle(projectRoot + "libs/org.eclipse.equinox.common_3.6.0.v20110523.jar"),
       bundle(projectRoot + "libs/org.eclipse.equinox.preferences_3.4.1.R37x_v20110725.jar"),
       bundle(projectRoot + "libs/org.eclipse.equinox.registry_3.5.101.R37x_v20110810-1611.jar"),
       bundle(projectRoot + "libs/org.eventb.core_2.4.0.r14093.jar"),
       bundle(projectRoot + "libs/org.eventb.core.ast_2.4.0.r14093.jar"),
       bundle(projectRoot + "libs/org.eventb.core.seqprover_2.4.0.r14093.jar"),
       bundle(projectRoot + "libs/org.rodinp.core_1.5.0.r14093.jar"),
       equinox().version("3.7.1")
     );
    }

    @Test
    public void checkBundleContext1() {
       assertNotNull(bundleContext);
    }

    @Test
    public void checkBundleContext2(BundleContext bc) {
       assertNotNull(bc);
    }

    @Test
    public void checkAccessToRodinDB() {
      RodinCore.getRodinDB();
    }
}

      

But I have the following problems:

  • both checkBundleContext methods failed, the associated context (using @Inject

    ) or passed as a parameter of the test method is always null

  • Accessing a static method getRodinDb()

    from a class RodinCore

    in a package is org.rodinp.core_1.5.0.r14093.jar

    throw a ClassNotFoundException

    about org.rodinp.core.RodinCore

    , even if the package org.rodinp.core

    is in the Export package of the package.

+3


source to share


2 answers


Check the import. Use

import javax.inject.Inject;

      



From Service Insert Page :

Injection is denoted by the javax.inject.Inject annotation. Pax Exam 1.x's own Inject annotation is now deprecated.

+6


source


check dependency:

<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-inject</artifactId>

      



it must be enabled during the pax-exam.

more details here

+2


source







All Articles