1

Below is my jUnit test from which I am trying to call afterPropertiesSet in my OSGiFramework class. But somehow getBundlesInformation method doesn't gets called during that flow.

When I debug my jUnit test, afterPropertiesSet method gets called and then it goes to initializeModelFramework method and then it never goes to getBundlesInformation method.

@Test
public void testOSGiFramework() {

    Method method;
    try {
        method = OSGiFramework.class.getDeclaredMethod("afterPropertiesSet", null);
        Object o = method.invoke(new OSGiFramework(), null);

        Assert.assertEquals(false, o instanceof Void);
    }
}

Below are the methods in OSGiFramework class-

private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

@Override
public void afterPropertiesSet() throws Exception {

    try {
        initializeModelFramework();
    }
}

private void initializeModelFramework() {

    final ScheduledFuture<?> taskHandle = scheduler.scheduleAtFixedRate(
            new Runnable() {
                public void run() {
                    try {
                        getBundlesInformation();
                    }catch(Exception ex) {
                        LOG.log(Level.SEVERE, "Exception in OSGiFramework::initializeModelFramework " +ex);
                        ex.printStackTrace();
                    }
                }
            }, 0, 30, TimeUnit.MINUTES);
}

protected static void getBundlesInformation() throws BundleException, Exception {
    System.out.println("Hello");
}

Does anyone know what might be the problem?

arsenal
  • 23,366
  • 85
  • 225
  • 331

2 Answers2

0

I think that since you didn't start the thread(Runnable object you defined), so the run method will never be executed. You have to start the thread for your run method to be executed.

Patrick B.
  • 1,257
  • 10
  • 18
  • Are you sure on this? Because that same code is working fine if I am running my application. But if I am invoking through jUnit test, then only it is not working.. – arsenal Sep 05 '13 at 20:05
0

Well the getBundlesInformation() method is being run on a different thread, so who's to say its not getting run AFTER the assert method or test? Either way, it seems the problem is to do with trying to Junit test a threaded application.

You might want to look into Junit testing with different threads. I've personally never done it but after a quick Google search it seems that its not a very simple tasks and its probably easier to avoid it/break the tests down so they don't have to deal with different threads.

Sorry if this answer wasn't of more use - if someone more experienced sees this please try and give a better answer!

Some quick searches brought me to:

Unit testing a multithreaded application?

Thread behaving strangely in JUnit

Good luck!

Community
  • 1
  • 1
DeaIss
  • 2,525
  • 7
  • 27
  • 37