Versions Compared

Key

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

...

Camel

...

Test

...

As

...

a

...

simple

...

alternative

...

to

...

using

...

Spring

...

Testing

...

or

...

Guice

...

the

...

camel-test

...

module

...

was

...

introduced

...

into

...

the

...

Camel

...

2.0

...

trunk

...

so

...

you

...

can

...

perform

...

powerful

...

Testing

...

of

...

your

...

Enterprise

...

Integration

...

Patterns

...

easily.

{:=
Info
tile
JUnit
or
TestNG
}

The

{{

camel-test

}}

JAR

is

using

JUnit.

There

is

an

alternative

{{

camel-testng

}}

JAR

(Camel

2.8

onwards)

using

the

[

TestNG

|http://testng.org/doc/index.html]

test

framework.

{info} h3. Adding to your

Adding to your pom.xml

...

To

...

get

...

started

...

using

...

Camel

...

Test

...

you

...

will

...

need

...

to

...

add

...

an

...

entry

...

to

...

your

...

pom.xml

JUnit

Code Block
xml
xml


h4. JUnit
{code:xml}
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-test</artifactId>
  <version>${camel-version}</version>
  <scope>test</scope>
</dependency>
{code}

h4. TestNG
*Available as of Camel 

TestNG

Available as of Camel 2.8

Code Block
xml
xml
*
{code:xml}
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-testng</artifactId>
  <version>${camel-version}</version>
  <scope>test</scope>
</dependency>
{code}

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


{code: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>
{code}

h3. Writing your test

You firstly need to derive from the class *CamelTestSupport* and typically you will need to override the *createRouteBuilder()* method to create routes to be tested.

Here is an [example|http://svn.apache.org/repos/asf/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns

Writing your test

You firstly need to derive from the class CamelTestSupport and typically you will need to override the createRouteBuilder() method to create routes to be tested.

Here is an example.

Wiki Markup
/FilterTest.java]. 

{snippet:lang=java|id=example|url=camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java}

Notice

...

how

...

you

...

can

...

use

...

the

...

various

...

Camel

...

binding

...

and

...

injection

...

annotations

...

to

...

inject

...

individual

...

Endpoint

...

objects

...

-

...

particularly

...

the

...

Mock

...

endpoints

...

which

...

are

...

very

...

useful

...

for

...

Testing

...

.

...

Also

...

you

...

can

...

inject

...

producer

...

objects

...

such

...

as

...

ProducerTemplate

...

or

...

some

...

application

...

code

...

interface

...

for

...

sending

...

messages

...

or

...

invoking

...

services.

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.

So you will also need to create a jndi.properties file in your src/test/resources directory so that there is a default registry available to initialise the CamelContext.

Here is an example jndi.properties file

Code Block



h3. JNDI

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

So you will also need to create a *jndi.properties* file in your *src/test/resources* directory so that there is a default registry available to initialise the [CamelContext].

Here is [an example jndi.properties file|http://svn.apache.org/repos/asf/camel/trunk/components/camel-test/src/test/resources/jndi.properties]

{code}
java.naming.factory.initial = org.apache.camel.util.jndi.CamelInitialContextFactory
{code}

h3. Dynamically assigning ports
*Available as of Camel 2.7*

Tests that 

Dynamically assigning ports

Available as of Camel 2.7

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

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

// 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);

{code}

{show-to:user=tjs}

Setup CamelContext once per class, or per every test method

Available as of Camel 2.8

The Camel Test kit will by default setup and shutdown CamelContext per every test method in your test class. So for example if you have 3 test methods, then CamelContext is started and shutdown after each test, that is 3 times.

You may want to do this once, to share the CamelContext between test methods, to speedup unit testing. This requires to use JUnit 4! In your unit test method you have to extend the org.apache.camel.test.junit4.CamelTestSupport test class and override the isCreateCamelContextPerClass method and return true as shown in the following example:

Wiki Markup
{snippet:id=e1example|lang=java|title=Setup CamelContext once per class|url=camel/trunk/components/camel-minatest/src/maintest/java/org/apache/camel/componenttest/minapatterns/MinaConsumerTestFilterCreateCamelContextPerClassTest.java}
{show-to}

h3. See Also

* [Testing]
* [Mock]


See Also