Over a million developers have joined DZone.

OSGi Unit Test Framework for Carbon 5

Here's a quick OSGi unit test framework example, complete with an intro, break down of Pax-Eaxm, and how to write a unit test case.

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale


In the current carbon kernel there is no way to write test cases in order to test the OSGi services and functions. This is purely because of the bundles needs to be installed and run on an OSGi  container. Pax- Exam [1] framework provides the underneath infrastructure to compose unit test cases which need to be run. 

How Pax-Exam Works

Pax exam starts up the OSGi framework, (which is equinox for the carbon kernel), with a minimum level of bundles which are necessary for the pax-exam operations. Then the Pax Exam user can provision their bundles to the OSGi container. Refer [2] for the provisioned bundles list [2]. This way when the Pax-Exam boots up, these provisioned bundles will be installed to the container.

How to Write Unit Test Case

Now the Pax-Exam has booted up the OSGi framework with the necessary bundles for your test environment. You have to use the @RunWith(PaxExam.class) annotation to hook Pax Exam into Testng and let it do its magic of setting up a test container with an OSGi framework and your bundles to run the tests.

[3] is a sample test case added. In the sample you can see that it is very similar to a normal testng test case and containing some annotations which relates to pax-exam. 

Using dependency injection, your test method can access the BundleContext of the probe bundle or any service obtained from the OSGi service registry.

    private BundleContext bundleContext;

or else you can directly access the OSGi services using the following 

    private SomeSampleOsgiService service;

If you need any special bundles other than those defined in [2], you can always override the config method in your test case class. But make sure that testng bundle must be installed.

    public Option[] config() {
        return options(
            mavenBundle("sample", "org.wso2.carbon", "1.0.0-SNAPSHOT")


 * Copyright 2015 WSO2, Inc. http://www.wso2.org
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

package org.wso2.carbon.osgi;

import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerClass;
import org.ops4j.pax.exam.testng.listener.PaxExam;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import javax.inject.Inject;

 * Base OSGi class to test the OSGi status of the org.wso2.carbon.core bundle
public class BaseOSGiTest {

    private BundleContext bundleContext;

    public void testBundleContextStatus() {
        Assert.assertNotNull(bundleContext, "Bundle Context is null");

    public void testCarbonCoreBundleStatus() {

        Bundle coreBundle = null;
        for (Bundle bundle : bundleContext.getBundles()) {
            if (bundle.getSymbolicName().equals("org.wso2.carbon.core")) {
                coreBundle = bundle;
        Assert.assertNotNull(coreBundle, "Carbon Core bundle not found");
        Assert.assertEquals(coreBundle.getState(), Bundle.ACTIVE, "Carbon Core Bundle is not activated");

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

enterprise integration,ei,integration,osgi,unit test,carbon 5

Published at DZone with permission of Aruna Karunarathna, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}