Steinar Bang a0168ea8de [maven-release-plugin] prepare for next development iteration | 6 years ago | |
---|---|---|
.. | ||
src | 7 years ago | |
README.org | 9 years ago | |
pom.xml | 6 years ago |
This is a simple mock implementation of parts of the OSGi framework, similar to what's found in Apache Sling OSGi mocks and Spring OSGi mocks.
The Apache Sling OSGi mocks were compiled against an older version of OSGi than what I was using, the Spring OSGi mocks didn't return a null reference for a non-existing service, and my tests required more logic in the classes than Mockito mocks could provide (basically: the tests needed a simple service registry).
Yes. The only dependencies it has is to OSGi core and the Java runtime.
Add the following dependency to your POM:
<project>
<dependencies>
<dependency>
<groupId>no.steria.osgi.jsr330activator</groupId>
<artifactId>jsr330activator.mocks</artifactId>
<version>1.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Then in the test, create a MockBundleContext and give it as an argument where a BundleContext is expected, e.g. with a BundleActivator:
public class MyOsgiTests() {
@Test
public void testMyActivator() {
BundleContext = new MockBundleContext();
MyActivator activator = new MyActivator();
// Check that the service can't be found before activator start
ServiceReference<?> helloBeforeActivation = bundleContext.getServiceReference(HelloService.class.getCanonicalName());
assertNull(helloBeforeActivation);
// Start the activator
activator.start();
// Verify that the service can now be found
ServiceReference<?> helloAfterActivation = bundleContext.getServiceReference(HelloService.class.getCanonicalName());
assertNotNull(helloAfterActivation);
// Get the service from the reference and call it
HelloService helloService = (HelloService) bundleContext.getService(helloAfterActivation);
assertEquals("Hello world!", helloService.getMessage());
// Unregister the service and verify that there is no service of the type present
activator.stop(bundleContext);
ServiceReference<?> helloAfterDeactivation = bundleContext.getServiceReference(HelloService.class.getCanonicalName());
assertNull(helloAfterDeactivation);
}
}