Apache
Home » Documentation » Development

OSGi Mocks

Mock implementation of selected OSGi APIs for easier testing.

Maven Dependency

1
2
3
4
<dependency>
  <groupId>org.apache.sling</groupId>
  <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
</dependency>

See latest version on the downloads page.

There are two major version ranges available:

Implemented mock features

The mock implementation supports:

Since osgi-mock 2.0.0:

Usage

OSGi Context JUnit Rule

The OSGi mock context can be injected into a JUnit test using a custom JUnit rule named OsgiContext. This rules takes care of all initialization and cleanup tasks required to make sure all unit tests can run independently (and in parallel, if required).

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class ExampleTest {

  @Rule
  public final OsgiContext context = new OsgiContext();

  @Test
  public void testSomething() {

    // register and activate service with configuration
    MyService service1 = context.registerInjectActivateService(new MyService(),
        "prop1", "value1");

    // get service instance
    OtherService service2 = context.getService(OtherService.class);

  }

}

It is possible to combine such a unit test with a @RunWith annotation e.g. for Mockito JUnit Runner.

The OsgiContext object provides access to mock implementations of:

Additionally it supports:

Getting OSGi mock objects

The factory class MockOsgi allows to instantiate the different mock implementations.

Example:

1
2
3
4
5
6
// get bundle context
BundleContext bundleContext = MockOsgi.newBundleContext();

// get component context with configuration
BundleContext bundleContext = MockOsgi.newComponentContext(properties,
    "prop1", "value1");

It is possible to simulate registering of OSGi services (backed by a simple hash map internally):

1
2
3
4
5
6
// register service
bundleContext.registerService(MyClass.class, myService, properties);

// get service instance
ServiceReference ref = bundleContext.getServiceReference(MyClass.class.getName());
MyClass service = bundleContext.getService(ref);

Activation and Dependency Injection

It is possible to simulate OSGi service activation, deactivation and dependency injection and the mock implementation tries to to its best to execute all as expected for an OSGi environment.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// get bundle context
BundleContext bundleContext = MockOsgi.newBundleContext();

// create service instance manually
MyService service = new MyService();

// inject dependencies
MockOsgi.injectServices(service, bundleContext);

// activate service
MockOsgi.activate(service, props);

// operate with service...

// deactivate service
MockOsgi.deactivate(service);

Please note:

Rev. 1761464 by sseifert on Mon, 19 Sep 2016 14:53:37 +0000
Apache Sling, Sling, Apache, the Apache feather logo, and the Apache Sling project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.