【问题标题】:How can I make a unit test that tests my Spring/OSGi configuration?如何进行单元测试来测试我的 Spring/OSGi 配置?
【发布时间】:2015-03-18 01:14:48
【问题描述】:
我有一个 bundle-context-osgi.xml 文件,其中列出了我的项目采用的服务和它发布的服务。我还有一个 bundle-context.xml,它定义了我的应用程序中定义的 bean。如何编写单元测试以确保所有内容都正确连接,并且我可以生产我的应用程序在服务器上时应该提供的服务?
注意:将其称为“单元测试”可能是错误的,但我认为这个想法很明确。
【问题讨论】:
标签:
java
spring
unit-testing
osgi
osgi-bundle
【解决方案1】:
对于 OSGi 集成测试,我通常使用 Pax 考试。它将启动一个 osgi 容器,发布所有配置的包,并从测试源创建一个小包,以部署为 OSGi 包。
有了它,您甚至可以通过@Inject 方式访问所有已注册的服务。
最好看看Pax Exam documentation。
简而言之,您创建一个新的集成测试模块,您可以在其中配置测试用例中的依赖项:
@RunWith(PaxExam.class)
@ExamReactorStrategy(PerMethod.class)
public class MyTest {
@Inject
protected BundleContext bundleContext;
@Inject
MyService service;
...
@Configuration
public Option[] configure() {
return options(
workingDirectory("target/paxexam/"),
cleanCaches(true),
junitBundles(),
mavenBundle().groupId("my.group.id")
.artifactId("my-artifact")
.version("1.0.0")
...
);
}
...
@Test
public void test() throws Exception {
assertNotNull(service);
}
}