2016-03-04 75 views
0

我正在开发使用Pax Exam 4.8,junit4,JBoss Fuse作为OSGi容器的一些OSGi bundle的集成测试。假设标准的Maven设置。如何访问Pax Exam考试测试套件中的资源?

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

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

@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考试中加载资源?如何检查资源是否包含在测试探针包中?

回答

1

通过热雷米的prosed溶液工作正常:
this.getClass().getClassLoader().getResourceAsStream("/myresource");

我的问题是在设置中。我把我的资源放在src/main/resources文件夹中。在构建探测器包时,Pax考试只包含src/test/resources文件夹中的资源。

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

1

你试过一个“简单”的getResourceAsStream()吗?这应该工作:

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

在我的集成测试的一个旧的一部分,我用下面的函数来获取“概率”捆绑(但说实话,我不认为这是现在有必要):

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

然后:

getProbeBundle().getResource(from).openStream()