I'm developing integration tests for some OSGi bundles using Pax Exam 4.8, junit4, JBoss Fuse as OSGi container. Assume standard Maven setup.
The container starts, my bundles are deployed and correctly started.
Now in my test code I need to load a resource and write its content to a file. If I understand correctly, the unit test is automatically deployed as test probe bundle.
@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
}
}
How do I load a resource inside a Pax Exam test? How can I check that the resource is included in the test probe bundle?
The prosed solution by Jérémie is working fine:
this.getClass().getClassLoader().getResourceAsStream("/myresource");
My problem was in the setup. I put my resources in src/main/resources
folder. Looks like Pax Exam includes only resources in src/test/resources
folder when building the probe bundle.
Afer moving the files in the correct folder, they can be accessed as resources in the usual way.