【问题标题】:BundleContext is null in unit tests using pax-examBundleContext 在使用 pax-exam 的单元测试中为空
【发布时间】:2012-04-07 21:52:05
【问题描述】:

我正在使用 pax-exam 加载、激活和访问 osgi 包。

以下源代码是我的 pax-exam 测试,它使用本地容器使用 pax-exam 2.3 运行。

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();
    }
}

但是我有以下问题:

  • 两个 checkBundleContext 方法都失败了,捆绑上下文注入(使用@Inject)或作为测试方法的参数传递始终为空

  • 从包org.rodinp.core_1.5.0.r14093.jar 中的类RodinCore 访问静态方法getRodinDb() 抛出一个关于org.rodinp.core.RodinCoreClassNotFoundException,即使包org.rodinp.core 在包的导出包中。

【问题讨论】:

    标签: java osgi pax-exam pax


    【解决方案1】:

    检查您的导入。使用

    import javax.inject.Inject;
    

    来自Service Injection page

    注入由 javax.inject.Inject 注释指示。帕克斯 考试自己的 1.x 版本行的 Inject 注释现在是 已弃用。

    【讨论】:

    • link 注入由 javax.inject.Inject 注释指示。
    • 是的!你是对的:正确的解决方案是使用javax.inject.Inject
    【解决方案2】:

    检查依赖:

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

    它应该包含在 pax-exam 测试中。

    更多详情here

    【讨论】:

    • 已添加,但仍无法正常工作:assertNotNull 抛出 AssertionError 并带有 bundleContext 注释 @Inject
    • 你能提供你的POM吗?
    猜你喜欢
    • 1970-01-01
    • 2013-11-19
    • 2012-04-29
    • 1970-01-01
    • 2016-03-19
    • 2014-03-17
    • 2013-11-22
    • 2015-01-12
    • 2015-09-13
    相关资源
    最近更新 更多