OSGi Fragment using Spring Integration Testing

720 views Asked by At

I am posting this question given I haven't found any relevant answer so far. I could not figure out how to run integration tests using Spring-Dynamic Modules.

Problem Definition

Template Application

  • One OSGi bundle containing the business logic. Using Spring DM to export / reference some services
  • One OSGi fragment for the Integration Tests. Tests should be able to inject helper test beans which are defined in the fragment Application Context (using xml files under META-INF/spring) as well as OSGi services exported (from the API bundle)

Versions

  • OSGi 3.6
  • Spring Framework 3.1.0
  • Spring Dynamic Modules 1.2.1

Question

  • How can I run Integration Test which would be aware of the Spring Application Context and of the exposed OSGi services? Using @RunWith(SpringJUnit4ClassRunner.class) and @ContextConfiguration(locations = { "classpath:META-INF/spring/context.xml" }) leads to a FileNotFoundException regarding context.xml, which exists... Debugging XmlBeanDefinitionReader confirmed my doubts: SpringJUnit4ClassRunner is trying to load the Application Context from its OSGi classpath, and cannot access my Bundle Resources.

How would you best solve this please?

I do appreciate your help in advance.

ps: I had a look at Spring DM Test but did not find anything relevant.

Example

Bundle Manifest

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloWorldDM
Bundle-SymbolicName: HelloWorldDM
Bundle-Version: 1.0.0.qualifier
Require-Bundle: org.eclipse.core.runtime,
 ContactDAO;bundle-version="1.0.0",
 org.springframework.osgi.extensions.annotations;bundle-version="1.2.1"
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Import-Package: ch.xxx.test.contact

Integration Test Fragment Manifest

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloWorldTest
Bundle-SymbolicName: HelloWorldTest
Bundle-Version: 1.0.0.qualifier
Fragment-Host: HelloWorldDM;bundle-version="1.0.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Require-Bundle: org.junit4;bundle-version="4.8.1",
 org.springframework.spring-test;bundle-version="3.1.0",
 org.springframework.spring-context;bundle-version="3.1.0",
 org.springframework.spring-beans;bundle-version="3.1.0",
 org.springframework.osgi.test;bundle-version="1.2.1",
 org.springframework.spring-core;bundle-version="3.1.0"

Fragment Application Context

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  
  <bean class="org.springframework.osgi.extensions.annotation.ServiceReferenceInjectionBeanPostProcessor"/>
  <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
  
</beans>

Test Class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:META-INF/spring/context.xml" })
public class TestCase {
    
    private BitemporalityService bitemporalityService;

    @ServiceReference
    public void setBitemporalityService(BitemporalityService service) {
        this.bitemporalityService = service;
    }

    @Test
    public void dummy(){
        System.err.println("in Test: ");
        assertNotNull(bitemporalityService);
    }

}
1

There are 1 answers

2
Christian Schneider On BEST ANSWER

Take a look at pax exam. It boots up an osgi container and runs your test in it. It is the defacto standard framework for osgi testing and is used in many open source projects for the osgi itests. See https://github.com/apache/karaf/tree/karaf/itests