camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Testing
Date Thu, 01 Mar 2012 17:40:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Testing">Testing</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~dvaleri">David
Valeri</a>
    </h4>
        <br/>
                         <h4>Changes (7)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-unchanged" >h2. Testing <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >Testing is a crucial activity
in any piece of software development or integration. Typically Camel Riders use various different
[technologies|Components] wired together in a variety of [patterns|Enterprise Integration
Patterns] with different [expression languages|Languages] together with different forms of
[Bean Integration] and [Dependency Injection] so its very easy for things to go <span class="diff-changed-words">wrong<span
class="diff-added-chars"style="background-color: #dfd;">\</span>! :-)<span class="diff-added-chars"style="background-color:
#dfd;"> </span>.</span> Testing is the crucial weapon to ensure that things
work as you would expect. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-changed-lines" >Camel is a Java library so you
can easily wire up tests in whatever unit testing framework you use (JUnit <span class="diff-changed-words">3.x<span
class="diff-added-chars"style="background-color: #dfd;"> (deprecated)</span>, 4.x<span
class="diff-added-chars"style="background-color: #dfd;">,</span></span> or
TestNG). However the Camel project has tried to make the testing of Camel as easy and powerful
as possible so we have introduced the following features. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h3. Testing mechanisms
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>|| Name || Component ||
Description || <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">|
[Camel Test] | {{camel-test}} | Is a standalone Java library letting you easily create Camel
test cases using a single Java class for all your configuration and routing without using
[Spring] or [Guice] for [Dependency Injection] which does not require an in depth knowledge
of Spring+SpringTest or Guice | <br>| [Spring Testing] | {{camel-test-spring}} | Uses
Spring Test together with either XML or Java Config to dependency inject your test classes.
Notice {{camel-test-spring}} is a new component in Camel 2.10 onwards. For older Camel release
use {{camel-test}} which has built-in [Spring Testing]. | <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">|
[Camel Test] | {{camel-test}} | Is a standalone Java library letting you easily create Camel
test cases using a single Java class for all your configuration and routing without using
[Spring] or [Guice] for [Dependency Injection]&amp;nbsp;which does not require an in-depth
knowledge of Spring + Spring Test or Guice. &amp;nbsp;Supports JUnit 3.x (deprecated)
and JUnit 4.x based tests. | <br>| [Spring Testing] | {{camel-test-spring}} | Supports
JUnit 3.x (deprecated) or JUnit 4.x based tests that bootstrap a test environment using Spring
without needing to be familiar with Spring Test. &amp;nbsp;The &amp;nbsp;plain JUnit
3.x/4.x based tests work very similar to the test support classes in camel-test. &amp;nbsp;Also
supports Spring Test based tests that use the declarative style of test configuration and
injection common in Spring Test. &amp;nbsp;The Spring Test based tests provide feature
parity with the plain JUnit 3.x/4.x based testing approach. &amp;nbsp;Notice {{camel-test-spring}}
is a new component in *Camel 2.10* onwards. For older Camel release use {{camel-test}} which
has built-in [Spring Testing]. | <br></td></tr>
            <tr><td class="diff-unchanged" >| [Blueprint Testing] | {{camel-test-blueprint}}
| *Camel 2.10:* Provides the ability to do unit testing on blueprint configurations | <br>|
[Guice] | {{camel-guice}} | Uses [Guice] to dependency inject your test classes | <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">|
Camel TestNG | camel-testng | Supports plain TestNG based tests&amp;nbsp;with or without&amp;nbsp;[Spring]&amp;nbsp;or&amp;nbsp;[Guice]&amp;nbsp;for&amp;nbsp;[Dependency
Injection]&amp;nbsp;which does not require an in-depth knowledge of Spring + Spring Test
or Guice. &amp;nbsp;Also from *Camel 2.10* onwards, this component supports Spring Test&amp;nbsp;based
tests that use the declarative style of test configuration and injection common in Spring
Test. | <br></td></tr>
            <tr><td class="diff-unchanged" > <br>In all approaches the test
classes look pretty much the same in that they all reuse the [Camel binding and injection
annotations|Bean Integration]. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >h4. Camel Test Example <br>
<br></td></tr>
            <tr><td class="diff-unchanged" >Here is the [Camel Test] [example|http://svn.apache.org/repos/asf/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java].
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>{snippet:lang=java|id=example|url=camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java}
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{snippet:lang=java|id=example|url=camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/DebugBlueprintTest.java}
<br> <br></td></tr>
            <tr><td class="diff-changed-lines" >Also notice the use of <span
class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">*{{getBlueprintDescriptors}}*</span>
<span class="diff-added-words"style="background-color: #dfd;">{{{*}getBlueprintDescriptors{*}}}</span>
to indicate that by default we should look for the [camelContext.xml in the package|http://svn.apache.org/repos/asf/camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/camelContext.xml]
to configure the test case which looks like this <br></td></tr>
            <tr><td class="diff-unchanged" > <br>{snippet:lang=xml|id=example|url=camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/camelContext.xml}
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >|| Name || Description || <br>|
[DataSet] | For load &amp; soak testing this endpoint provides a way to create huge numbers
of messages for sending to [Components] and asserting that they are consumed correctly | <br></td></tr>
            <tr><td class="diff-unchanged" >| [Mock] | For testing routes and
mediation rules using mocks and allowing assertions to be added to an endpoint | <br></td></tr>
            <tr><td class="diff-unchanged" >| [Test] | Creates a [Mock] endpoint
which expects to receive all the message bodies that could be polled from the given underlying
endpoint | <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>|| Name || Description
|| <br></td></tr>
            <tr><td class="diff-changed-lines" >| [Direct] | <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">
</span> Direct invocation of the consumer from the producer so that single threaded
(non-SEDA) in VM invocation is performed which can be useful to mock out physical transports
| <br></td></tr>
            <tr><td class="diff-unchanged" >| [SEDA] | Delivers messages asynchonously
to consumers via a [java.util.concurrent.BlockingQueue|http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/BlockingQueue.html]
which is good for testing asynchronous transports | <br> <br>h3. Testing existing
routes <br> <br></td></tr>
            <tr><td class="diff-unchanged" >Camel provides some features to aid
during testing of existing routes where you cannot or will not use [Mock] etc. For example
you may have a production ready route which you want to test with some 3rd party API which
sends messages into this route. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>|| Name || Description
|| <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Testing-Testing"></a>Testing</h2>

<p>Testing is a crucial activity in any piece of software development or integration.
Typically Camel Riders use various different <a href="/confluence/display/CAMEL/Components"
title="Components">technologies</a> wired together in a variety of <a href="/confluence/display/CAMEL/Enterprise+Integration+Patterns"
title="Enterprise Integration Patterns">patterns</a> with different <a href="/confluence/display/CAMEL/Languages"
title="Languages">expression languages</a> together with different forms of <a
href="/confluence/display/CAMEL/Bean+Integration" title="Bean Integration">Bean Integration</a>
and <a href="/confluence/display/CAMEL/Dependency+Injection" title="Dependency Injection">Dependency
Injection</a> so its very easy for things to go wrong&#33; <img class="emoticon"
src="/confluence/images/icons/emoticons/smile.gif" height="20" width="20" align="absmiddle"
alt="" border="0"/> . Testing is the crucial weapon to ensure that things work as you would
expect.</p>

<p>Camel is a Java library so you can easily wire up tests in whatever unit testing
framework you use (JUnit 3.x (deprecated), 4.x, or TestNG). However the Camel project has
tried to make the testing of Camel as easy and powerful as possible so we have introduced
the following features.</p>

<h3><a name="Testing-Testingmechanisms"></a>Testing mechanisms</h3>

<p>The following mechanisms are supported</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Component </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/Camel+Test" title="Camel
Test">Camel Test</a> </td>
<td class='confluenceTd'> <tt>camel-test</tt> </td>
<td class='confluenceTd'> Is a standalone Java library letting you easily create Camel
test cases using a single Java class for all your configuration and routing without using
<a href="/confluence/display/CAMEL/Spring" title="Spring">Spring</a> or <a
href="/confluence/display/CAMEL/Guice" title="Guice">Guice</a> for <a href="/confluence/display/CAMEL/Dependency+Injection"
title="Dependency Injection">Dependency Injection</a>&nbsp;which does not require
an in-depth knowledge of Spring + Spring Test or Guice. &nbsp;Supports JUnit 3.x (deprecated)
and JUnit 4.x based tests. </td>
</tr>
<tr>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/Spring+Testing" title="Spring
Testing">Spring Testing</a> </td>
<td class='confluenceTd'> <tt>camel-test-spring</tt> </td>
<td class='confluenceTd'> Supports JUnit 3.x (deprecated) or JUnit 4.x based tests that
bootstrap a test environment using Spring without needing to be familiar with Spring Test.
&nbsp;The &nbsp;plain JUnit 3.x/4.x based tests work very similar to the test support
classes in camel-test. &nbsp;Also supports Spring Test based tests that use the declarative
style of test configuration and injection common in Spring Test. &nbsp;The Spring Test
based tests provide feature parity with the plain JUnit 3.x/4.x based testing approach. &nbsp;Notice
<tt>camel-test-spring</tt> is a new component in <b>Camel 2.10</b>
onwards. For older Camel release use <tt>camel-test</tt> which has built-in <a
href="/confluence/display/CAMEL/Spring+Testing" title="Spring Testing">Spring Testing</a>.
</td>
</tr>
<tr>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/Blueprint+Testing" title="Blueprint
Testing">Blueprint Testing</a> </td>
<td class='confluenceTd'> <tt>camel-test-blueprint</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> Provides the ability to do
unit testing on blueprint configurations </td>
</tr>
<tr>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/Guice" title="Guice">Guice</a>
</td>
<td class='confluenceTd'> <tt>camel-guice</tt> </td>
<td class='confluenceTd'> Uses <a href="/confluence/display/CAMEL/Guice" title="Guice">Guice</a>
to dependency inject your test classes </td>
</tr>
<tr>
<td class='confluenceTd'> Camel TestNG </td>
<td class='confluenceTd'> camel-testng </td>
<td class='confluenceTd'> Supports plain TestNG based tests&nbsp;with or without&nbsp;<a
href="/confluence/display/CAMEL/Spring" title="Spring">Spring</a>&nbsp;or&nbsp;<a
href="/confluence/display/CAMEL/Guice" title="Guice">Guice</a>&nbsp;for&nbsp;<a
href="/confluence/display/CAMEL/Dependency+Injection" title="Dependency Injection">Dependency
Injection</a>&nbsp;which does not require an in-depth knowledge of Spring + Spring
Test or Guice. &nbsp;Also from <b>Camel 2.10</b> onwards, this component supports
Spring Test&nbsp;based tests that use the declarative style of test configuration and
injection common in Spring Test. </td>
</tr>
</tbody></table>
</div>


<p>In all approaches the test classes look pretty much the same in that they all reuse
the <a href="/confluence/display/CAMEL/Bean+Integration" title="Bean Integration">Camel
binding and injection annotations</a>.</p>

<h4><a name="Testing-CamelTestExample"></a>Camel Test Example</h4>

<p>Here is the <a href="/confluence/display/CAMEL/Camel+Test" title="Camel Test">Camel
Test</a> <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java"
class="external-link" rel="nofollow">example</a>.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class FilterTest
<span class="code-keyword">extends</span> CamelTestSupport {

    @EndpointInject(uri = <span class="code-quote">"mock:result"</span>)
    <span class="code-keyword">protected</span> MockEndpoint resultEndpoint;

    @Produce(uri = <span class="code-quote">"direct:start"</span>)
    <span class="code-keyword">protected</span> ProducerTemplate template;

    @Test
    <span class="code-keyword">public</span> void testSendMatchingMessage() <span
class="code-keyword">throws</span> Exception {
        <span class="code-object">String</span> expectedBody = <span class="code-quote">"&lt;matched/&gt;"</span>;

        resultEndpoint.expectedBodiesReceived(expectedBody);

        template.sendBodyAndHeader(expectedBody, <span class="code-quote">"foo"</span>,
<span class="code-quote">"bar"</span>);

        resultEndpoint.assertIsSatisfied();
    }

    @Test
    <span class="code-keyword">public</span> void testSendNotMatchingMessage()
<span class="code-keyword">throws</span> Exception {
        resultEndpoint.expectedMessageCount(0);

        template.sendBodyAndHeader(<span class="code-quote">"&lt;notMatched/&gt;"</span>,
<span class="code-quote">"foo"</span>, <span class="code-quote">"notMatchedHeaderValue"</span>);

        resultEndpoint.assertIsSatisfied();
    }

    @Override
    <span class="code-keyword">protected</span> RouteBuilder createRouteBuilder()
{
        <span class="code-keyword">return</span> <span class="code-keyword">new</span>
RouteBuilder() {
            <span class="code-keyword">public</span> void configure() {
                from(<span class="code-quote">"direct:start"</span>).filter(header(<span
class="code-quote">"foo"</span>).isEqualTo(<span class="code-quote">"bar"</span>)).to(<span
class="code-quote">"mock:result"</span>);
            }
        };
    }
}
</pre>
</div></div>

<p>Notice how it derives from the Camel helper class <b>CamelTestSupport</b>
but has no Spring or Guice dependency injection configuration but instead overrides the <b>createRouteBuilder()</b>
method.</p>

<h4><a name="Testing-SpringTestwithXMLConfigExample"></a>Spring Test with
XML Config Example</h4>

<p>Here is the <a href="/confluence/display/CAMEL/Spring+Testing" title="Spring Testing">Spring
Testing</a> <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/patterns/FilterTest.java"
class="external-link" rel="nofollow">example using XML Config</a>.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">@ContextConfiguration
<span class="code-keyword">public</span> class FilterTest <span class="code-keyword">extends</span>
AbstractJUnit38SpringContextTests {

    @EndpointInject(uri = <span class="code-quote">"mock:result"</span>)
    <span class="code-keyword">protected</span> MockEndpoint resultEndpoint;

    @Produce(uri = <span class="code-quote">"direct:start"</span>)
    <span class="code-keyword">protected</span> ProducerTemplate template;

    @DirtiesContext
    <span class="code-keyword">public</span> void testSendMatchingMessage() <span
class="code-keyword">throws</span> Exception {
        <span class="code-object">String</span> expectedBody = <span class="code-quote">"&lt;matched/&gt;"</span>;

        resultEndpoint.expectedBodiesReceived(expectedBody);

        template.sendBodyAndHeader(expectedBody, <span class="code-quote">"foo"</span>,
<span class="code-quote">"bar"</span>);

        resultEndpoint.assertIsSatisfied();
    }

    @DirtiesContext
    <span class="code-keyword">public</span> void testSendNotMatchingMessage()
<span class="code-keyword">throws</span> Exception {
        resultEndpoint.expectedMessageCount(0);

        template.sendBodyAndHeader(<span class="code-quote">"&lt;notMatched/&gt;"</span>,
<span class="code-quote">"foo"</span>, <span class="code-quote">"notMatchedHeaderValue"</span>);

        resultEndpoint.assertIsSatisfied();
    }
}
</pre>
</div></div>

<p>Notice that we use <b>@DirtiesContext</b> on the test methods to force
<a href="/confluence/display/CAMEL/Spring+Testing" title="Spring Testing">Spring Testing</a>
to automatically reload the <a href="/confluence/display/CAMEL/CamelContext" title="CamelContext">CamelContext</a>
after each test method - this ensures that the tests don't clash with each other (e.g. one
test method sending to an endpoint that is then reused in another test method).</p>

<p>Also notice the use of <b>@ContextConfiguration</b> to indicate that
by default we should look for the <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/patterns/FilterTest-context.xml"
class="external-link" rel="nofollow">FilterTest-context.xml on the classpath</a>
to configure the test case which looks like this</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">&lt;beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
       <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
       <span class="code-keyword">xmlns:context</span>=<span class="code-quote">"http://www.springframework.org/schema/context"</span>
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
    "&gt;

  <span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
    <span class="code-tag">&lt;route&gt;</span>
      <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
      <span class="code-tag">&lt;filter&gt;</span>
        <span class="code-tag">&lt;xpath&gt;</span>$foo = 'bar'<span
class="code-tag">&lt;/xpath&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
      <span class="code-tag">&lt;/filter&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
  <span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<h4><a name="Testing-SpringTestwithJavaConfigExample"></a>Spring Test with
Java Config Example</h4>

<p>Here is the <a href="/confluence/display/CAMEL/Spring+Testing" title="Spring Testing">Spring
Testing</a> <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/patterns/FilterTest.java"
class="external-link" rel="nofollow">example using Java Config</a>. For more information
see <a href="/confluence/display/CAMEL/Spring+Java+Config" title="Spring Java Config">Spring
Java Config</a>.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">@ContextConfiguration(
        locations = <span class="code-quote">"org.apache.camel.spring.javaconfig.patterns.FilterTest$ContextConfig"</span>,
        loader = JavaConfigContextLoader.class)
<span class="code-keyword">public</span> class FilterTest <span class="code-keyword">extends</span>
AbstractJUnit4SpringContextTests {

    @EndpointInject(uri = <span class="code-quote">"mock:result"</span>)
    <span class="code-keyword">protected</span> MockEndpoint resultEndpoint;

    @Produce(uri = <span class="code-quote">"direct:start"</span>)
    <span class="code-keyword">protected</span> ProducerTemplate template;

    @DirtiesContext
    @Test
    <span class="code-keyword">public</span> void testSendMatchingMessage() <span
class="code-keyword">throws</span> Exception {
        <span class="code-object">String</span> expectedBody = <span class="code-quote">"&lt;matched/&gt;"</span>;

        resultEndpoint.expectedBodiesReceived(expectedBody);

        template.sendBodyAndHeader(expectedBody, <span class="code-quote">"foo"</span>,
<span class="code-quote">"bar"</span>);

        resultEndpoint.assertIsSatisfied();
    }

    @DirtiesContext
    @Test
    <span class="code-keyword">public</span> void testSendNotMatchingMessage()
<span class="code-keyword">throws</span> Exception {
        resultEndpoint.expectedMessageCount(0);

        template.sendBodyAndHeader(<span class="code-quote">"&lt;notMatched/&gt;"</span>,
<span class="code-quote">"foo"</span>, <span class="code-quote">"notMatchedHeaderValue"</span>);

        resultEndpoint.assertIsSatisfied();
    }

    @Configuration
    <span class="code-keyword">public</span> <span class="code-keyword">static</span>
class ContextConfig <span class="code-keyword">extends</span> SingleRouteCamelConfiguration
{
        @Bean
        <span class="code-keyword">public</span> RouteBuilder route() {
            <span class="code-keyword">return</span> <span class="code-keyword">new</span>
RouteBuilder() {
                <span class="code-keyword">public</span> void configure() {
                    from(<span class="code-quote">"direct:start"</span>).filter(header(<span
class="code-quote">"foo"</span>).isEqualTo(<span class="code-quote">"bar"</span>)).to(<span
class="code-quote">"mock:result"</span>);
                }
            };
        }
    }
}
</pre>
</div></div>

<p>This is similar to the XML Config example above except that there is no XML file
and instead the nested <b>ContextConfig</b> class does all of the configuration;
so your entire test case is contained in a single Java class. We currently have to reference
by class name this class in the <b>@ContextConfiguration</b> which is a bit ugly.
Please vote for <a href="http://jira.springframework.org/browse/SJC-238" class="external-link"
rel="nofollow">SJC-238</a> to address this and make Spring Test work more cleanly
with Spring JavaConfig.</p>

<p>Its totally optional but for the ContextConfig implementation we derive from <b>SingleRouteCamelConfiguration</b>
which is a helper Spring Java Config class which will configure the CamelContext for us and
then register the RouteBuilder we create.</p>

<h4><a name="Testing-BlueprintTest"></a>Blueprint Test</h4>

<p>Here is the <a href="/confluence/display/CAMEL/Blueprint+Testing" title="Blueprint
Testing">Blueprint Testing</a> <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/DebugBlueprintTest.java"
class="external-link" rel="nofollow">example using XML Config</a>.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// to use camel-test-blueprint,
then extend the CamelBlueprintTestSupport class,
</span><span class="code-comment">// and add your unit tests methods as shown
below.
</span><span class="code-keyword">public</span> class DebugBlueprintTest
<span class="code-keyword">extends</span> CamelBlueprintTestSupport {

    <span class="code-comment">// override <span class="code-keyword">this</span>
method, and <span class="code-keyword">return</span> the location of our Blueprint
XML file to be used <span class="code-keyword">for</span> testing
</span>    @Override
    <span class="code-keyword">protected</span> <span class="code-object">String</span>
getBlueprintDescriptor() {
        <span class="code-keyword">return</span> <span class="code-quote">"org/apache/camel/test/blueprint/camelContext.xml"</span>;
    }

    <span class="code-comment">// here we have regular Junit @Test method
</span>    @Test
    <span class="code-keyword">public</span> void testRoute() <span class="code-keyword">throws</span>
Exception {
        <span class="code-comment">// set mock expectations
</span>        getMockEndpoint(<span class="code-quote">"mock:a"</span>).expectedMessageCount(1);

        <span class="code-comment">// send a message
</span>        template.sendBody(<span class="code-quote">"direct:start"</span>,
<span class="code-quote">"World"</span>);

        <span class="code-comment">// <span class="code-keyword">assert</span>
mocks
</span>        assertMockEndpointsSatisfied();
    }

}
</pre>
</div></div>

<p>Also notice the use of <tt><b>getBlueprintDescriptors</b></tt>
to indicate that by default we should look for the <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/camelContext.xml"
class="external-link" rel="nofollow">camelContext.xml in the package</a> to configure
the test case which looks like this</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">&lt;blueprint xmlns=<span class="code-quote">"http://www.osgi.org/xmlns/blueprint/v1.0.0"</span>
           <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
           xsi:schemaLocation="
             http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"&gt;

  <span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/blueprint"</span>&gt;</span>

    <span class="code-tag">&lt;route&gt;</span>
      <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
      <span class="code-tag">&lt;transform&gt;</span>
        <span class="code-tag">&lt;simple&gt;</span>Hello ${body}<span
class="code-tag">&lt;/simple&gt;</span>
      <span class="code-tag">&lt;/transform&gt;</span>
      <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:a"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>

  <span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;/blueprint&gt;</span>
</pre>
</div></div>

<h3><a name="Testing-Testingendpoints"></a>Testing endpoints</h3>

<p>Camel provides a number of endpoints which can make testing easier.</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/DataSet" title="DataSet">DataSet</a>
</td>
<td class='confluenceTd'> For load &amp; soak testing this endpoint provides a way
to create huge numbers of messages for sending to <a href="/confluence/display/CAMEL/Components"
title="Components">Components</a> and asserting that they are consumed correctly
</td>
</tr>
<tr>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/Mock" title="Mock">Mock</a>
</td>
<td class='confluenceTd'> For testing routes and mediation rules using mocks and allowing
assertions to be added to an endpoint </td>
</tr>
<tr>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/Test" title="Test">Test</a>
</td>
<td class='confluenceTd'> Creates a <a href="/confluence/display/CAMEL/Mock" title="Mock">Mock</a>
endpoint which expects to receive all the message bodies that could be polled from the given
underlying endpoint </td>
</tr>
</tbody></table>
</div>


<p>The main endpoint is the <a href="/confluence/display/CAMEL/Mock" title="Mock">Mock</a>
endpoint which allows expectations to be added to different endpoints; you can then run your
tests and assert that your expectations are met at the end.</p>

<h3><a name="Testing-Stubbingoutphysicaltransporttechnologies"></a>Stubbing
out physical transport technologies</h3>

<p>If you wish to test out a route but want to avoid actually using a real physical
transport (for example to unit test a transformation route rather than performing a full integration
test) then the following endpoints can be useful.</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/Direct" title="Direct">Direct</a>
</td>
<td class='confluenceTd'> Direct invocation of the consumer from the producer so that
single threaded (non-SEDA) in VM invocation is performed which can be useful to mock out physical
transports </td>
</tr>
<tr>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/SEDA" title="SEDA">SEDA</a>
</td>
<td class='confluenceTd'> Delivers messages asynchonously to consumers via a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/BlockingQueue.html"
class="external-link" rel="nofollow">java.util.concurrent.BlockingQueue</a> which
is good for testing asynchronous transports </td>
</tr>
</tbody></table>
</div>


<h3><a name="Testing-Testingexistingroutes"></a>Testing existing routes</h3>

<p>Camel provides some features to aid during testing of existing routes where you cannot
or will not use <a href="/confluence/display/CAMEL/Mock" title="Mock">Mock</a>
etc. For example you may have a production ready route which you want to test with some 3rd
party API which sends messages into this route.</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/NotifyBuilder" title="NotifyBuilder">NotifyBuilder</a>
</td>
<td class='confluenceTd'> Allows you to be notified when a certain condition has occurred.
For example when the route has completed 5 messages. You can build complex expressions to
match your criteria when to be notified. </td>
</tr>
<tr>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/AdviceWith" title="AdviceWith">AdviceWith</a>
</td>
<td class='confluenceTd'> Allows you to <b>advice</b> or <b>enhance</b>
an existing route using a <a href="/confluence/display/CAMEL/RouteBuilder" title="RouteBuilder">RouteBuilder</a>
style. For example you can add interceptors to intercept sending outgoing messages to assert
those messages are as expected. </td>
</tr>
</tbody></table>
</div>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Testing">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=104032&revisedVersion=21&originalVersion=20">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Testing?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message