使用Pax考试测试内部课程

时间:2018-12-28 13:34:47

标签: junit osgi osgi-bundle pax-exam pax-runner

目前,我有用于使用Pax-Exam 5测试内部类的测试类。

@ProbeBuilder
public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
   probe.setHeader("Fragment-Host", "com.mycompany.abc");
   return probe;
}

我使用Fragment-Host的原因是避免为包com.mycompany.abc导出内部包。 但是我得到这个错误

org.ops4j.pax.exam.TestContainerException: org.osgi.framework.BundleException: Invalid operation on a fragment.
    at org.ops4j.pax.exam.nat.internal.NativeTestContainer.install(NativeTestContainer.java:135)
    at org.ops4j.pax.exam.nat.internal.NativeTestContainer.install(NativeTestContainer.java:140)
    at org.ops4j.pax.exam.nat.internal.NativeTestContainer.installProbe(NativeTestContainer.java:428)
    at org.ops4j.pax.exam.spi.reactors.EagerSingleStagedReactor.setUp(EagerSingleStagedReactor.java:68)
    at org.ops4j.pax.exam.spi.reactors.EagerSingleStagedReactor.beforeClass(EagerSingleStagedReactor.java:106)
    at org.ops4j.pax.exam.spi.reactors.ReactorManager.beforeClass(ReactorManager.java:400)
    at org.ops4j.pax.exam.junit.DriverExtension.beforeClassBlock(DriverExtension.java:130)
    at org.ops4j.pax.exam.junit.ExtensibleRunner$1.evaluate(ExtensibleRunner.java:53)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.ops4j.pax.exam.junit.PaxExam.run(PaxExam.java:78)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: org.osgi.framework.BundleException: Invalid operation on a fragment.
    at org.eclipse.osgi.container.Module.checkFragment(Module.java:520)
    at org.eclipse.osgi.container.Module.start(Module.java:408)
    at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:428)
    at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:447)
    at org.ops4j.pax.exam.nat.internal.NativeTestContainer.install(NativeTestContainer.java:131)
... 15 more

还有其他解决方法吗?

谢谢

1 个答案:

答案 0 :(得分:0)

Pax考试支持分片的预配,因为您可以在预配选项上调用.noStart()来告诉Pax考试不得开始捆绑/片段:

CoreOptions.mavenBundle("gid", "aid", "version").noStart();

但是您要达到的目标是不同的,您正在尝试将探针包变成测试包的一部分。

您仍然可以实现您的目标,而不必按如下方式将探针片段化:

  1. 使用TinyBundles创建一个片段,以导出测试包的内部包
  2. 使用.noStart()作为url()选项将片段传递给Pax考试

有关示例,请参见以下类:

https://github.com/ops4j/org.ops4j.pax.exam2/blob/exam-reactor-3.2.0/itest/osgi/src/it/regression-multi/src/test/java/org/ops4j/pax/exam/regression/multi/fragment/FragmentTest.java#L61

换句话说,不是使探针成为宿主的片段,而是动态创建一个空片段,只是为了使探针受益而导出宿主的软件包。

免责声明:我没有尝试过,这只是一个假设。