Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

To get started using Camel Test you will need to add an entry to your pom.xml

JUnit

Code Block
xml
xml

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-test</artifactId>
  <version>${camel-version}</version>
  <scope>test</scope>
</dependency>

...

Available as of Camel 2.8

Code Block
xml
xml

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-testng</artifactId>
  <version>${camel-version}</version>
  <scope>test</scope>
</dependency>

You might also want to add slf4j and log4j to ensure nice logging messages (and maybe adding a log4j.properties file into your src/test/resources directory).

Code Block
xml
xml

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <scope>test</scope>
</dependency>

...

Method Name

Description

Default Behavior

boolean isUseRouteBuilder()

If the route builders from returned from createRouteBuilder() or createRouteBuilders() should be added to the CamelContext used in the test should be started.

Returns true.  createRouteBuilder() or createRouteBuilders() are invoked and the CamelContext is started automatically.

boolean isUseAdviceWith()

If the CamelContext use in the test should be automatically started before test methods are invoked.
Override when using advice with and return true.  This helps in knowing the adviceWith is to be used, and the CamelContext will not be started before the advice with takes place. This delay helps by ensuring the advice with has been property setup before the CamelContext is started.

Info

Its important to start the CamelContext manually from the unit test after you are done doing all the advice with.

Returns false.  the CamelContext is started automatically before test methods are invoked.

boolean isCreateCamelContextPerClass()

See Setup CamelContext once per class, or per every test method.

The CamelContext and routes are recreated for each test method.

String isMockEndpoints()

Triggers the auto-mocking of endpoints whose URIs match the provided filter.  The default filter is null which disables this feature.  Return "*"  to match all endpoints.  See org.apache.camel.impl.InterceptSendToMockEndpointStrategy for more details on the registration of the mock endpoints.

Disabled

boolean isUseDebugger()

If this method returns true, the debugBefore(Exchange exchange, Processor processor, ProcessorDefinition<?> definition, String id, String label) and 
debugAfter(Exchange exchange, Processor processor, ProcessorDefinition<?> definition, String id, String label, long timeTaken) methods are invoked for each processor in the registered routes.

Disabled.  The methods are not invoked during the test.

int getShutdownTimeout()

Returns the number of seconds that Camel should wait for graceful shutdown.  Useful for decreasing test times when a message is still in flight at the end of the test.

Returns 10 seconds.

boolean useJmx()

If JMX should be disabled on the CamelContext used in the test.

JMX is disabled.

JndiRegistry createRegistry()

Provides a hook for adding objects into the registry.  Override this method to bind objects to the registry before test methods are invoked.

An empty registry is initialized.

useOverridePropertiesWithPropertiesComponent

Camel 2.10: Allows to add/override properties when Using PropertyPlaceholder in Camel.

null

ignoreMissingLocationWithPropertiesComponent

Camel 2.10: Allows to control if Camel should ignore missing locations for properties.

null

boolean isDumpRouteStatsCamel 2.16: If enabled, then Camel will dump all route statistics into XML files in the target/camel-route-stats directory. These XML files contains information about "route coverage" of all the routes that was used during the unit test. This allows tooling to inspect these XML files and generate nice route coverage reports.Disabled.

JNDI

Camel uses a Registry to allow you to configure Component or Endpoint instances or Beans used in your routes. If you are not using Spring or OSGi then JNDI is used as the default registry implementation.

...

Here is an example jndi.properties file

...

Dynamically assigning ports

...

Tests that use port numbers will fail if that port is already on use. AvailablePortFinder provides methods for finding unused port numbers at runtime.

Code Block

// Get the next available port number starting from the default starting port of 1024
int port1 = AvailablePortFinder.getNextAvailable();
/*
 * Get another port. Note that just getting a port number does not reserve it so
 * we look starting one past the last port number we got.
 */
int port2 = AvailablePortFinder.getNextAvailable(port1 + 1);

...

Wiki Markup
{snippet:id=example|lang=java|title=Setup CamelContext once per class|url=camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterCreateCamelContextPerClassTest.java}

See Also

Code Block
java.naming.factory.initial = org.apache.camel.util.jndi.CamelInitialContextFactory