2

我正在使用 Pax Exam 4.8、junit4、JBoss Fuse 作为 OSGi 容器为一些 OSGi 包开发集成测试。假设标准 Maven 设置。

容器启动,我的包已部署并正确启动。

现在在我的测试代码中,我需要加载资源并将其内容写入文件。如果我理解正确,单元测试会自动部署为测试探针包。

@RunWith(PaxExam.class)
@ExamReactorStrategy(PerMethod.class)
public class ExampleTest {

    @javax.inject.Inject
    BundleContext bundleContext;

    @Configuration
    public Option[] config() throws Exception {
        // container setup
    }

    @Test
    public void testThatApplicationProcessesThisFile() {
        InputStream is1 = getClass().getResourceAsStream("myResource");  // returns null

        Bundle probeBundle = bundleContext.getBundle("local");
        InputStream is2 = probeBundle.getResource("myResource").openStream();
        // getResource() returns null

        // write the resource as a file
    }

}

如何在 Pax Exam 测试中加载资源?如何检查资源是否包含在测试探针包中?

4

2 回答 2

2

Jérémie 提出的解决方案运行良好:
this.getClass().getClassLoader().getResourceAsStream("/myresource");

我的问题出在设置中。我把我的资源放在src/main/resources文件夹中。看起来 Pax Examsrc/test/resources在构建探测包时仅包含文件夹中的资源。

将文件移动到正确的文件夹后,就可以以通常的方式将它们作为资源进行访问。

于 2016-03-08T10:11:13.560 回答
1

您是否尝试过“简单”的 getResourceAsStream() ?这应该工作:

this.getClass().getClassLoader().getResourceAsStream("/myresource");

在我的集成测试的旧部分中,我使用以下函数来获取“prob”包(但老实说,我认为现在没有必要):

private Bundle getProbeBundle() {
    for (Bundle bundle : bundleContext.getBundles()) {
        if (bundle.getSymbolicName().startsWith("PAXEXAM-PROBE-")) {
            return bundle;
        }
    }
    return null;
}

接着 :

getProbeBundle().getResource(from).openStream()
于 2016-03-04T12:47:23.510 回答