OSGi and friends methods

Developing OSGi tests can be definitely boring. First, testing is generally not a very exciting experience, but imagine if you have to handle all the OSGi issues in your tests... Don't worry junit4osgi provides methods allowing to interact easily with OSGi!

{div:class=toc} [TOC]

OSGi methods

junit4osgi test case extends the OSGiTestCase class. This class provides useful methods allowing to find services, get them, get the PackageAdmin service... The description of these methods can be found afterward.

The most part of the methods are available statically and non-statically. Static methods require a Bundle. Non static methods use the current bundle context, and track get services to release them when the test is done. So, we advise you to use the non-static methods. Static methods just allow you to check that a specific bundle can access to services / resources.

Service interaction

Get the bundle context

Install/Start/Uninstall bundles


Extensibility: Helper objects

junit4osgi provides an extensibility mechanism to reduce the pain of testing. So, if you're interacting with specific services or environment, you can use Helper objects. Those object have to be created in the setUp method and disposed in the tearDown method.

So, for example, if you write iPOJO tests, you can use the iPOJO helper providing a lot of utility functions simplifying the development of tests. {code:java} public class MyTest extends OSGiTestCase { ComponentInstance fooProvider1, fooProvider2;

IPOJOHelper helper; // Helper object

public void setUp() {
    helper = new IPOJOHelper(this); // Create the object.   
String type2 = "PS-FooProviderType";

fooProvider1 = helper.createComponentInstance(type2, p3);
    fooProvider2 = helper.createComponentInstance(type2, "FooProvider-4");

public void tearDown() {
helper.dispose(); // Dispose it, instances will be disposed too.

You can also implements your own helper (for specific purpose) by just implementing the  interface.