Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

Blueprint Testing

Available as of Camel 2.10

Testing is a crucial part of any development or integration work. Camel supports the definition of Blueprint routes, but given Blueprint is an OSGi specific technology, writing unit tests is quite difficult. This library leverages PojoSR which provides a service registry without using a fully compliant OSGi container. This allows defining real unit tests (as opposed to integration tests using Pax Exam. Please make sure all test jars in you class path are OSGi bundle.

Wiki Markup
{snippet:lang=java|id=example|url=camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/DebugBlueprintTest.java}

Also notice the use of getBlueprintDescriptor to specify the location of the OSGi Blueprint XML file.
If you have multiple OSGi Blueprint XML files, then you can specify them with a comma-separated list in the getBlueprintDescriptor method.

Here's the Blueprint XML file:

Wiki Markup
{snippet:lang=xml|id=example|url=camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/camelContext.xml}

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>

Setting timeout when getting CamelContext

Available as of Camel 2.13.0/2.12.1/2.11.2

CamelBlueprintTestSupport waits 30 sec for Camel Context to be ready by default, now you can override this value in two ways:

  • Globally, by setting org.apache.camel.test.blueprint.camelContextCreationTimeout system property.
  • Locally for each test, by overriding getCamelContextCreationTimeout method.

Adding services on startup

Available as of Camel 2.11.2/2.12.0

When using camel-test-blueprint you may do unit tests which requires using shared services which is not available during unit testing, but only in the real OSGi container, for example a shared DataSource.

To make it easier to register services on startup, such as a standalone DataSource or any other service, you can override the method addServicesOnStartup when your unit test class extends CamelBlueprintTestSupport.

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>