1

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

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

我使用 Fragment-Host 的原因是为了避免为 bundle 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

还有其他解决方案吗?

谢谢

4

1 回答 1

0

Pax Exam 支持片段.noStart()的配置,因为您可以调用配置选项来告诉 Pax Exam 不能启动捆绑包/片段:

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

但是您要实现的目标不同,您正在尝试将您的探测包变成测试包的一个片段。

您仍然可以实现您的目标,而无需使探针成为片段,如下所示:

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

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

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

换句话说,与其使探针成为主机的片段,不如动态创建一个空片段,只是为了导出主机的包以使探针受益。

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

于 2019-01-16T13:31:21.493 回答