2

I want to know if anyone has tried to test beans/services exposed through blueprint.xml working in pax-exam using native container.

I have a project with two bundles - a) config - interface classes b) config-impl - contains implementation and exposes bean as service defined in blueprint.xml.

I was hoping that @Inject in the test class similar to approach mentioned @ https://ops4j1.jira.com/wiki/display/PAXEXAM3/Getting+Started+with+OSGi+Tests should automatically set the instance value in @Inject'ed variable but it doesn't seem to be working.

The options sent to pax-exam are pasted below. By any chance, would there be more bundles to load so that pax-exam starts recognizing blueprint.xml and boot up the service?

    return options(
            systemProperty("osgi.console").value("6666"),
            junitBundles(),
            provision(
                    mavenBundle("org.osgilab.testing", "commons", "1.0.0"),
                    mavenBundle("org.apache.commons", "com.springsource.org.apache.commons.codec", "1.3.0"),
                    mavenBundle("org.codehaus.jackson", "jackson-core-asl", "1.9.12"),
                    mavenBundle("org.codehaus.jackson", "jackson-mapper-asl", "1.9.12"),
                    mavenBundle("com.umum.container", "container-config", "1.0.0"),
                    mavenBundle("com.umum.container", "container-config-impl", "1.0.0").start()),

            systemProperty("pax.exam.service.timeout").value("160000"), systemTimeout(160000));
Unic Man
  • 41
  • 4
  • Your basic aproach of using @Inject looks good. Can you post your project or a simplified project to github or similar so we can take a look? – Christian Schneider Jul 09 '13 at 17:51
  • Thanks for quick reply! I won't be able to share real code. Let me create a test project and have it shared someway. Give me a day or two. – Unic Man Jul 10 '13 at 06:36

2 Answers2

5

I use the following system bundles:

static Option systemBundles() {
  return composite(
    mavenBundle( "org.apache.aries.blueprint", "org.apache.aries.blueprint", "1.0.0" ),
    mavenBundle( "org.apache.aries", "org.apache.aries.util", "1.0.0" ),
    mavenBundle( "org.apache.aries.proxy", "org.apache.aries.proxy", "1.0.0" ),
    junitBundles(),
    cleanCaches( true ) );
}

Plus my own bundles, so my complete Config looks similar to this:

@Configuration
Option[] config( ) {
    return options(
      javaFxPackages(),
      systemBundles(),
      mavenBundle( "org.codehaus.groovy", "groovy-all", "2.1.1" ) );
}

All my services get injected correctly. For example, I can get the BundleContext service like this:

@Inject BundleContext context;

Hope this works for you too :)

Renato
  • 12,940
  • 3
  • 54
  • 85
  • 1
    You can also delegate version numbers to the Maven build using `mavenBundle("org.apache.aries", "org.apache.aries.util").versionAsInProject(), mavenBundle("org.apache.aries.blueprint", "org.apache.aries.blueprint").versionAsInProject(), mavenBundle("org.apache.aries.proxy", "org.apache.aries.proxy").versionAsInProject()` and the **depends** plugin: https://ops4j1.jira.com/wiki/display/paxexam/Pax+Exam+-+Tutorial+1 (see Tutorial 1 Session 4) – Jim Bethancourt Jan 13 '14 at 22:24
-1

Pax Exam doesn't care how OSGi services get registered, you can use Blueprint, Declarative Services or do it manually.

When a test doesn't seem to work, there's two things to check:

  • Are the services registered at all? Use an OSGi console/shell to check.
  • Does your Pax Exam setup include all required JARs?

Pax Exam's own integration tests can serve as an example for setting up a test environment.

Harald Wellmann
  • 12,615
  • 4
  • 41
  • 63
  • Thanks for quick reply! I tried to start osgi console using systemProperty("osgi.console").value("6666") inside options(..) in pax exam 3.1 based unit test. I still can't telnet to the console. It's weird. Do you know how to start console? I am basically running native container - felix. – Unic Man Jul 10 '13 at 06:38
  • "Pax Exam doesn't care how OSGi services get registered, you can use Blueprint, Declarative Services or do it manually." -- the module under test has blueprint.xml. What else is required to start the service manually? Any pointer you can give? – Unic Man Jul 10 '13 at 06:47
  • With Felix, you need to provision additional bundles to get a shell (search for "Gogo shell"). That's the main reason why I prefer Equinox due to its built-in shell (up to 3.7.x). – Harald Wellmann Jul 10 '13 at 17:14
  • I will check out Gogo shell. But other question i have is how to configure equinox. I see all articles talking about using equinox() and that's it. But I can't find it in ConfigOptions class anymore. – Unic Man Jul 11 '13 at 09:28
  • `equinox()` was for the Pax Runner Test container only, a Pax Exam 2.x feature which was removed in 3.x. The OSGi framework is selected implicitly by putting a given implementation on the classpath (using `META-INF/services` lookup of a `FrameworkFactory`). – Harald Wellmann Jul 15 '13 at 17:04