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:

Provide your own configuration via ConfigAdmin

If you want to provide your own configuration to an OSGi service that you do not register and activate itself in the mock context you can provide your own custom OSGi configuration via the mock implementation of the ConfigAdmin service.

Example:

1
2
3
4
5
ConfigurationAdmin configAdmin = context.getService(ConfigurationAdmin.class);
Configuration myServiceConfig = configAdmin.getConfiguration(MY_SERVICE_PID);
Dictionary<String, Object> props = new Hashtable<String, Object>();
props.put("prop1", "value1");
myServiceConfig.update(props);

Context Plugins

OSGi Mocks supports "Context Plugins" that hook into the lifecycle of each test run and can prepare test setup before or after the other setUp actions, and execute test tear down code before or after the other tearDown action.

To define a plugin implement the org.apache.sling.testing.mock.osgi.context.ContextPlugin<OsgiContextImpl> interface. For convenience it is recommended to extend the abstract class org.apache.sling.testing.mock.osgi.context.AbstractContextPlugin<OsgiContextImpl>. These plugins can be used with OSGi Mock context, but also with context instances deriving from it like Sling Mocks and AEM Mocks. In most cases you would just override the afterSetUp method. In this method you can register additional OSGi services or do other preparation work. It is recommended to define a constant pointing to a singleton of a plugin instance for using it.

To use a plugin in your unit test class, use the OsgiContextBuilder class instead of directly instantiating the OsgiContextclass. This allows you in a fluent style to configure more options, with the plugin(...) method you can add one or more plugins.

Example:

1
2
@Rule
public OsgiContext context = new OsgiContextBuilder().plugin(MY_PLUGIN).build();

More examples:

Rev. 1777318 by sseifert on Wed, 4 Jan 2017 14:04:18 +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.