Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8072657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:05:19+00:00 2026-06-05T14:05:19+00:00

I’m trying to figure out how to implement multi-bundle integration test in OSGi using

  • 0

I’m trying to figure out how to implement multi-bundle integration test in OSGi using JUnit.

With integration test, I mean instantiating a subset of the bundles to automatically validate functionality in that subsystem.

We’re running Equinox and using Eclipse as toolchain. Eclipse offers the “Run as JUnit Plug-in” option which brings the OSGi framework up and instantiates the configures bundles, so I guess this is the path to follow, but I don’t find a way to inject DS references into my tests.
I’ve seen the use of the ServiceTracker as a programmatic means to access the different service bundles, but that beats the purpose of having DS, isn’t it?

I am just getting started with OSGI, so I figure I’m just missing some piece of the puzzle that would let me put my multi-bundle tests together.

Any ideas?

Thanks, Gerard.

* EDIT : SOLUTION *

After looking further into this issue, I finally figured out how to put this mult-bundle integration tests in place using the JUnit plug-in feature:

For the dynamic services injection to work, one must create a service definition file where the injected dependencies must be declared, as it’s usually done when working with DS.
This file goes (typically) under the OSGI-INF/ directory. e.g. OSGI-INF/service.xml

service.xml must declare the required dependencies for this test, but does not offer a service of its own:

service.xml
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" immediate="true" name="MyTest" activate="startup" deactivate="shutdown">

   <implementation class="com.test.functionaltest.MyTester"/>
   <reference name="OtherService" interface="com.product.service.FooService" policy="static" cardinality="1..1" bind="onServiceUp" unbind="onServiceDown"/>

</scr:component>

This will instruct DS to inject the dependency on FooService using the declared onServiceUp method. onServiceDown must be implemented as it’s called during the OSGi shutdown phase after the tests are run.

com.test.functionaltest.MyTester contains the test methods to be executed, following the typical JUnit practices.

Up to here, it’s all ‘by the book’. Yet, if the Junit is run, it will throw NullPointerException when accessing a reference to FooService. The reason for that is that the OSGi framework is in a race condition with the JUnit tests runner context, and usually, the Junit test runner wins that race, executing the tests before the reference to the required service is injected.

To solve this situation, it’s required to make the Junit test to wait for the OSGi runtime to do its work. I addressed this issue by using a CountDownLatch, that is initialized to the number of dependent services required in the test.
Then every dependency injection method counts down and when they are all done, the test will start. The code looks like this:

private static CountDownLatch dependencyLatch = new CountDownLatch(1);// 1 = number of dependencies required    
static FooService  fooService = null;   
public void onFooServiceUp(FooService service) {
  fooService = service;
  dependencyLatch.countDown();
}

Note that the fooService reference needs to be static to allow sharing the service reference between the OSGi and the JUnit execution contexts. The CountDownLatch provides a high-level synchronization mechanism for safe publication of this shared reference.

Then, a dependency check should be added before the test execution:

@Before
public void dependencyCheck() {
  // Wait for OSGi dependencies
    try {
      dependencyLatch.await(10, TimeUnit.SECONDS); 
      // Dependencies fulfilled
    } catch (InterruptedException ex)  {
      fail("OSGi dependencies unfulfilled");
    }
}

This way the Junit framework waits for the OSGi DS service to inject the dependencies or fails after the timeout.

It took me quite some time to completely figure this one out. I hope it saves some headache to fellow programmers in the future.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:05:20+00:00Added an answer on June 5, 2026 at 2:05 pm

    * EDIT : SOLUTION *

    After looking further into this issue, I finally figured out how to put this mult-bundle integration tests in place using the JUnit plug-in feature:

    For the dynamic services injection to work, one must create a service definition file where the injected dependencies must be declared, as it’s usually done when working with DS.
    This file goes (typically) under the OSGI-INF/ directory. e.g. OSGI-INF/service.xml

    service.xml must declare the required dependencies for this test, but does not offer a service of its own:

    service.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" immediate="true" name="MyTest" activate="startup" deactivate="shutdown">
    
       <implementation class="com.test.functionaltest.MyTester"/>
       <reference name="OtherService" interface="com.product.service.FooService" policy="static" cardinality="1..1" bind="onServiceUp" unbind="onServiceDown"/>
    
    </scr:component>
    

    This will instruct DS to inject the dependency on FooService using the declared onServiceUp method. onServiceDown must be implemented as it’s called during the OSGi shutdown phase after the tests are run.

    com.test.functionaltest.MyTester contains the test methods to be executed, following the typical JUnit practices.

    Up to here, it’s all ‘by the book’. Yet, if the Junit is run, it will throw NullPointerException when accessing a reference to FooService. The reason for that is that the OSGi framework is in a race condition with the JUnit tests runner context, and usually, the Junit test runner wins that race, executing the tests before the reference to the required service is injected.

    To solve this situation, it’s required to make the Junit test to wait for the OSGi runtime to do its work. I addressed this issue by using a CountDownLatch, that is initialized to the number of dependent services required in the test.
    Then every dependency injection method counts down and when they are all done, the test will start. The code looks like this:

    private static CountDownLatch dependencyLatch = new CountDownLatch(1);// 1 = number of dependencies required    
    static FooService  fooService = null;   
    public void onFooServiceUp(FooService service) {
      fooService = service;
      dependencyLatch.countDown();
    }
    

    Note that the fooService reference needs to be static to allow sharing the service reference between the OSGi and the JUnit execution contexts. The CountDownLatch provides a high-level synchronization mechanism for safe publication of this shared reference.

    Then, a dependency check should be added before the test execution:

    @Before
    public void dependencyCheck() {
      // Wait for OSGi dependencies
        try {
          dependencyLatch.await(10, TimeUnit.SECONDS); 
          // Dependencies fulfilled
        } catch (InterruptedException ex)  {
          fail("OSGi dependencies unfulfilled");
        }
    }
    

    This way the Junit framework waits for the OSGi DS service to inject the dependencies or fails after the timeout.

    It took me quite some time to completely figure this one out. I hope it saves some headache to fellow programmers in the future.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.