对于PaxExam(版本4)中的配置,我们正在使用wrappedBundle()
,如下所示:
wrappedBundle(mavenBundle().groupId("com.github.tomakehurst").artifactId("wiremock-jre8").versionAsInProject()),
因为我们想用普通的jar创建一个OSGi包。
然后为了使用包装机制,我们必须安装包装功能:
features(karafStandardRepo, "wrap"),
问题是当安装wrappedBundle()
时,包装功能尚未出现。如何确保PaxExam配置wrappedBundle()
仅在包装功能存在并准备好使用后执行?我们在此测试中使用的Karaf发行版是4.0.7版。
谢谢你的帮助,Kladderadatsch
是的,确实我们必须通过PaxUrl Wrap机制将WireMock捆绑包生成包装到一个单独的功能文件中:
<features name="wiremock-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
<feature name="wiremock" version="${project.version}">
<feature prerequisite="true">wrap</feature>
<bundle>
wrap:mvn:com.github.tomakehurst/wiremock-jre8-standalone/2.21.0$Bundle-ClassPath=.
</bundle>
</feature>
</features>
非常重要的是正确配置XML命名空间,即解决版本v1.4.0,否则prerequisite
没用。我之前介入的另一个陷阱是没有采用独立版本的WireMock。
然后在PaxExam配置中我刚刚安装了该功能:
features(maven().groupId("com.company.wiremock").artifactId("wiremock-feature").type("xml").classifier("features").version("1.0.0-SNAPSHOT"), "wiremock"),
当你在测试中初始化WireMockServer时,为了可以通过ClassLoader.getResource()
(该库的内部东西)加载新生成的WireMock-Bundle中的资源,你必须在你的测试中这样做,否则Bundle-Classloader没有使用WireMock-Bundle:
@BeforeClass
public static void setup() {
ClassLoader savedClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(WireMockClassRule.class.getClassLoader());
wireMockServer = new WireMockServer(options().dynamicPort());
wireMockServer.start();
} finally {
Thread.currentThread().setContextClassLoader(savedClassLoader);
}
}
@AfterClass
public static void end() {
wireMockServer.stop();
}
您可以将它放在JUnit @ClassRule
中进行封装。