<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>6.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.utils</artifactId>
<version>1.12.0</version>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
properties
org.osgi.framework.storage.clean=onFirstInit
felix.cache.profiledir=${user.dir}/target/felix-cache
org.osgi.framework.startlevel.beginning=100
org.osgi.framework.storage=${java.io.tmpdir}/felix-cache
org.osgi.framework.storage.clean=onFirstInit
felix.fileinstall.noInitialDelay=true
felix.fileinstall.dir=${user.dir}/bundles
import org.apache.felix.framework.Felix;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
public class OSGiTestRunner {
private Felix framework;
public void start() throws BundleException {
framework = new Felix(getFrameworkConfig());
framework.init();
framework.start();
}
public void stop() throws BundleException {
framework.stop();
framework.waitForStop(0);
framework = null;
}
private Map<String, Object> getFrameworkConfig() {
Map<String, Object> config = new HashMap<>();
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
config.put(Constants.FRAMEWORK_STORAGE, System.getProperty("java.io.tmpdir") + "/felix-cache");
return config;
}
public void installBundle(String bundlePath) throws BundleException {
Bundle bundle = framework.getBundleContext().installBundle(bundlePath);
bundle.start();
}
}
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import static org.junit.Assert.*;
public class IntegrationTest {
private static OSGiTestRunner testRunner;
@BeforeClass
public static void setUp() throws BundleException {
testRunner = new OSGiTestRunner();
testRunner.start();
testRunner.installBundle("path/to/your/test-bundle.jar");
}
@AfterClass
public static void tearDown() throws BundleException {
testRunner.stop();
}
@Test
public void testBundleService() {
BundleContext bundleContext = testRunner.getFramework().getBundleContext();
ServiceReference<ServiceInterface> serviceRef = bundleContext.getServiceReference(ServiceInterface.class);
ServiceInterface service = bundleContext.getService(serviceRef);
assertNotNull(service);
assertEquals("expected result", service.doSomething());
}
}
mvn test