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 19:21: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 (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<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-unchanged" >| 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" >Its totally optional but for the ContextConfig
implementation we derive from *SingleRouteCamelConfiguration* which is a helper Spring Java
Config class which will configure the CamelContext for us and then register the RouteBuilder
we create. <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h4.
Spring Test with XML Config and Declarative Configuration Example <br> <br>Here
is a Camel test support enhanced&amp;nbsp;[Spring Testing]&amp;nbsp;[example using
XML Config and pure Spring Test based configuration of the Camel Context|https://svn.apache.org/repos/asf/camel/trunk/components/camel-test-spring/src/test/java/org/apache/camel/test/junit4/CamelSpringJUnit4ClassRunnerPlainTest.java].
<br> <br>{snippet:lang=java\|id=e1\|url=camel/trunk/components/camel-test-spring/src/test/java/org/apache/camel/test/junit4/CamelSpringJUnit4ClassRunnerPlainTest.java}
<br> <br>Notice how a custom test runner is used with the&amp;nbsp;*@RunWith*&amp;nbsp;annotation
to support the features of&amp;nbsp;*CamelTestSupport*&amp;nbsp;through annotations
on the test class. <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h4. Blueprint Test <br> <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 <br class="atl-forced-newline" /> </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-SpringTestwithXMLConfigandDeclarativeConfigurationExample"></a>Spring
Test with XML Config and Declarative Configuration Example</h4>

<p>Here is a Camel test support enhanced&nbsp;<a href="/confluence/display/CAMEL/Spring+Testing"
title="Spring Testing">Spring Testing</a>&nbsp;<a href="https://svn.apache.org/repos/asf/camel/trunk/components/camel-test-spring/src/test/java/org/apache/camel/test/junit4/CamelSpringJUnit4ClassRunnerPlainTest.java"
class="external-link" rel="nofollow">example using XML Config and pure Spring Test based
configuration of the Camel Context</a>.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration
<span class="code-comment">// Put here to prevent Spring context caching across tests
and test methods since some tests inherit 
</span><span class="code-comment">// from <span class="code-keyword">this</span>
test and therefore use the same Spring context.  Also because we want to reset the
</span><span class="code-comment">// Camel context and mock endpoints between
test methods automatically.
</span>@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
<span class="code-keyword">public</span> class CamelSpringJUnit4ClassRunnerPlainTest
{
    
    @Autowired
    <span class="code-keyword">protected</span> CamelContext camelContext;
    
    @Autowired
    <span class="code-keyword">protected</span> CamelContext camelContext2;
    
    @EndpointInject(uri = <span class="code-quote">"mock:a"</span>, context =
<span class="code-quote">"camelContext"</span>)
    <span class="code-keyword">protected</span> MockEndpoint mockA;
    
    @EndpointInject(uri = <span class="code-quote">"mock:b"</span>, context =
<span class="code-quote">"camelContext"</span>)
    <span class="code-keyword">protected</span> MockEndpoint mockB;
    
    @EndpointInject(uri = <span class="code-quote">"mock:c"</span>, context =
<span class="code-quote">"camelContext2"</span>)
    <span class="code-keyword">protected</span> MockEndpoint mockC;
    
    @Produce(uri = <span class="code-quote">"direct:start"</span>, context = <span
class="code-quote">"camelContext"</span>)
    <span class="code-keyword">protected</span> ProducerTemplate start;
    
    @Produce(uri = <span class="code-quote">"direct:start2"</span>, context =
<span class="code-quote">"camelContext2"</span>)
    <span class="code-keyword">protected</span> ProducerTemplate start2;
    
    @Test
    <span class="code-keyword">public</span> void testPositive() <span class="code-keyword">throws</span>
Exception {
        assertEquals(ServiceStatus.Started, camelContext.getStatus());
        assertEquals(ServiceStatus.Started, camelContext2.getStatus());
        
        mockA.expectedBodiesReceived(<span class="code-quote">"David"</span>);
        mockB.expectedBodiesReceived(<span class="code-quote">"Hello David"</span>);
        mockC.expectedBodiesReceived(<span class="code-quote">"David"</span>);
        
        start.sendBody(<span class="code-quote">"David"</span>);
        start2.sendBody(<span class="code-quote">"David"</span>);
        
        MockEndpoint.assertIsSatisfied(camelContext);
    }
    
    @Test
    <span class="code-keyword">public</span> void testJmx() <span class="code-keyword">throws</span>
Exception {
        assertEquals(DefaultManagementStrategy.class, camelContext.getManagementStrategy().getClass());
    }
    
    @Test
    <span class="code-keyword">public</span> void testShutdownTimeout() <span
class="code-keyword">throws</span> Exception {
        assertEquals(10, camelContext.getShutdownStrategy().getTimeout());
        assertEquals(TimeUnit.SECONDS, camelContext.getShutdownStrategy().getTimeUnit());
    }
    
    @Test
    <span class="code-keyword">public</span> void testStopwatch() {
        StopWatch stopWatch = StopWatchTestExecutionListener.getStopWatch();
        
        assertNotNull(stopWatch);
        assertTrue(stopWatch.taken() &lt; 100);
    }
    
    @Test
    <span class="code-keyword">public</span> void testExcludedRoute() {
        assertNotNull(camelContext.getRoute(<span class="code-quote">"excludedRoute"</span>));
    }
    
    @Test
    <span class="code-keyword">public</span> void testProvidesBreakpoint() {
        assertNull(camelContext.getDebugger());
        assertNull(camelContext2.getDebugger());
    }

    @SuppressWarnings(<span class="code-quote">"deprecation"</span>)
    @Test
    <span class="code-keyword">public</span> void testLazyLoadTypeConverters()
{
        assertTrue(camelContext.isLazyLoadTypeConverters());
        assertTrue(camelContext2.isLazyLoadTypeConverters());
    }
}
</pre>
</div></div>

<p>Notice how a custom test runner is used with the&nbsp;<b>@RunWith</b>&nbsp;annotation
to support the features of&nbsp;<b>CamelTestSupport</b>&nbsp;through annotations
on the test class.</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=22&originalVersion=21">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