Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added section about classpath scanning.

...

In order to define blueprint tests, add the following dependency in your pom:

Code Block
xml
xml

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-test-blueprint</artifactId>
  <version>2.10</version>
  <scope>test</scope>
</dependency>

Classpath scanning

By default PojoSR test container scans the test classpath for all the OSGi bundles available there. All the bundles with Blueprint descriptor files will be automatically started by the test container. If you would like to prevent particular bundles from being started by the test container, override the getBundleFilter method, just as demonstrated on the snippet below. 

Code Block
xml
xml
@Override
protected String getBundleFilter() {
  // I don't want test container to scan and load Logback bundle during the test
  return "(!(Bundle-SymbolicName=ch.qos.logback.core))";
}


Keep in mind that not specifying the Blueprint descriptor in the getBlueprintDescriptor method will not prevent the test container from loading given descriptor. Bundle filter method is the proper way of filtering out bundles you don't want to start during the test.

Setting timeout when getting CamelContext

...

In the example below we register a service org.apache.camel.test.blueprint.MyService using the name myService having a property beer=Carlsberg, as shown below:

Code Block

    @Override
    protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) {
        services.put("myService", asService(myService, "beer", "Carlsberg"));
    }

The asService is a builder method that makes it easy to register a service with a single property. If you need more properties you can use the asService method that takes a Dictionary as argument. And if you do not need any properties, then just pass in null, eg:

Code Block

services.put("myService", asService(myService, null));

This allows us to use the service by calling a method on it from a Camel Bean component in a route as shown:

Code Block
xml
xml

    <route>
      <from uri="direct:start"/>
      <to uri="bean:myService"/>
      <to uri="mock:result"/>
    </route>

Notice the bean endpoint uses the service name myService which was the name we registered the service as. You can also use the fully qualified class name instead, which is more common with OSGi.

Code Block

    @Override
    protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) {
        services.put(MyService.class.getName(), asService(myService, "beer", "Carlsberg"));
    }

And in the route we use the FQN name:

Code Block
xml
xml

    <route>
      <from uri="direct:start"/>
      <to uri="bean:org.apache.camel.test.blueprint.MyService"/>
      <to uri="mock:result"/>
    </route>