Steinar Bang a0168ea8de [maven-release-plugin] prepare for next development iteration 6 лет назад
..
src dc8d54f9c6 Merge branch 'add-named-inject-support' into add-optional-injection-support 7 лет назад
README.org 641501f068 Link to javadoc created by travis ci builds. 9 лет назад
pom.xml a0168ea8de [maven-release-plugin] prepare for next development iteration 6 лет назад

README.org

What's this?

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.

Why does it exist?

Can it be used without the jsr330Activator?

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.

How do I use it?

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);
      }
  }

Is there documentation?