camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Daniel Kulp (Confluence)" <conflue...@apache.org>
Subject [CONF] Apache Camel > Testing
Date Fri, 28 Jun 2013 18:40:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/en/2176/1/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/~dkulp">Daniel
Kulp</a>
    </h4>
        <br/>
                         <h4>Changes (2)</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" >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/spring/CamelSpringJUnit4ClassRunnerPlainTest.java].
<br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">{snippet:lang=java\|id=e1\|url=camel/trunk/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringJUnit4ClassRunnerPlainTest.java}
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{snippet:lang=java|id=e1|url=camel/trunk/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringJUnit4ClassRunnerPlainTest.java}
<br></td></tr>
            <tr><td class="diff-unchanged" > <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. &amp;nbsp;See&amp;nbsp;[Spring
Testing]&amp;nbsp;for a list of annotations you can use in your tests. <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 and described in more detail under <a href="/confluence/display/CAMEL/Spring+Testing"
title="Spring Testing">Spring Testing</a>. </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">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
public class FilterTest extends CamelTestSupport {

    @EndpointInject(uri = "mock:result")
    protected MockEndpoint resultEndpoint;

    @Produce(uri = "direct:start")
    protected ProducerTemplate template;

    @Test
    public void testSendMatchingMessage() throws Exception {
        String expectedBody = "&lt;matched/&gt;";

        resultEndpoint.expectedBodiesReceived(expectedBody);

        template.sendBodyAndHeader(expectedBody, "foo", "bar");

        resultEndpoint.assertIsSatisfied();
    }

    @Test
    public void testSendNotMatchingMessage() throws Exception {
        resultEndpoint.expectedMessageCount(0);

        template.sendBodyAndHeader("&lt;notMatched/&gt;", "foo", "notMatchedHeaderValue");

        resultEndpoint.assertIsSatisfied();
    }

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {
                from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");
            }
        };
    }
}
]]></script>
</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">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
@ContextConfiguration
public class FilterTest extends SpringRunWithTestSupport {

    @EndpointInject(uri = "mock:result")
    protected MockEndpoint resultEndpoint;

    @Produce(uri = "direct:start")
    protected ProducerTemplate template;

    @DirtiesContext
    @Test
    public void testSendMatchingMessage() throws Exception {
        String expectedBody = "&lt;matched/&gt;";

        resultEndpoint.expectedBodiesReceived(expectedBody);

        template.sendBodyAndHeader(expectedBody, "foo", "bar");

        resultEndpoint.assertIsSatisfied();
    }

    @DirtiesContext
    @Test
    public void testSendNotMatchingMessage() throws Exception {
        resultEndpoint.expectedMessageCount(0);

        template.sendBodyAndHeader("&lt;notMatched/&gt;", "foo", "notMatchedHeaderValue");

        resultEndpoint.assertIsSatisfied();
    }
}
]]></script>
</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">
<script type="syntaxhighlighter" class="theme: Default; brush: xml; gutter: false"><![CDATA[
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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;

  &lt;camelContext xmlns="http://camel.apache.org/schema/spring"&gt;
    &lt;route&gt;
      &lt;from uri="direct:start"/&gt;
      &lt;filter&gt;
        &lt;xpath&gt;$foo = 'bar'&lt;/xpath&gt;
        &lt;to uri="mock:result"/&gt;
      &lt;/filter&gt;
    &lt;/route&gt;
  &lt;/camelContext&gt;

&lt;/beans&gt;
]]></script>
</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>. </p>

<p>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">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
@ContextConfiguration(
        locations = "org.apache.camel.spring.javaconfig.patterns.FilterTest$ContextConfig",
        loader = JavaConfigContextLoader.class)
public class FilterTest extends AbstractJUnit4SpringContextTests {

    @EndpointInject(uri = "mock:result")
    protected MockEndpoint resultEndpoint;

    @Produce(uri = "direct:start")
    protected ProducerTemplate template;

    @DirtiesContext
    @Test
    public void testSendMatchingMessage() throws Exception {
        String expectedBody = "&lt;matched/&gt;";

        resultEndpoint.expectedBodiesReceived(expectedBody);

        template.sendBodyAndHeader(expectedBody, "foo", "bar");

        resultEndpoint.assertIsSatisfied();
    }

    @DirtiesContext
    @Test
    public void testSendNotMatchingMessage() throws Exception {
        resultEndpoint.expectedMessageCount(0);

        template.sendBodyAndHeader("&lt;notMatched/&gt;", "foo", "notMatchedHeaderValue");

        resultEndpoint.assertIsSatisfied();
    }

    @Configuration
    public static class ContextConfig extends SingleRouteCamelConfiguration {
        @Bean
        public RouteBuilder route() {
            return new RouteBuilder() {
                public void configure() {
                    from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");
                }
            };
        }
    }
}
]]></script>
</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>

<p>Since <b>Camel 2.11.0</b> you can use the CamelSpringJUnit4ClassRunner
with CamelSpringDelegatingTestContextLoader like <a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/CamelSpringDelegatingTestContextLoaderTest.java"
class="external-link" rel="nofollow">example using Java Config with CamelSpringJUnit4ClassRunner</a>.<br/>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(
        classes = {CamelSpringDelegatingTestContextLoaderTest.TestConfig.class},
        // Since Camel 2.11.0 
        loader = CamelSpringDelegatingTestContextLoader.class
)
@MockEndpoints
public class CamelSpringDelegatingTestContextLoaderTest {
    @EndpointInject(uri = "mock:direct:end")
    protected MockEndpoint endEndpoint;

    @EndpointInject(uri = "mock:direct:error")
    protected MockEndpoint errorEndpoint;

    @Produce(uri = "direct:test")
    protected ProducerTemplate testProducer;

    @Configuration
    public static class TestConfig extends SingleRouteCamelConfiguration {
        @Bean
        @Override
        public RouteBuilder route() {
            return new RouteBuilder() {
                @Override
                public void configure() throws Exception {
                    from("direct:test").errorHandler(deadLetterChannel("direct:error")).to("direct:end");

                    from("direct:error").log("Received message on direct:error endpoint.");

                    from("direct:end").log("Received message on direct:end endpoint.");
                }
            };
        }
    }

    @Test
    public void testRoute() throws InterruptedException {
        endEndpoint.expectedMessageCount(1);
        errorEndpoint.expectedMessageCount(0);

        testProducer.sendBody("&lt;name&gt;test&lt;/name&gt;");

        endEndpoint.assertIsSatisfied();
        errorEndpoint.assertIsSatisfied();
    }
}
]]></script>
</div></div>.</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/spring/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">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration
// Put here to prevent Spring context caching across tests and test methods since some tests
inherit 
// from this test and therefore use the same Spring context.  Also because we want to reset
the
// Camel context and mock endpoints between test methods automatically.
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class CamelSpringJUnit4ClassRunnerPlainTest {
    
    @Autowired
    protected CamelContext camelContext;
    
    @Autowired
    protected CamelContext camelContext2;
    
    @EndpointInject(uri = "mock:a", context = "camelContext")
    protected MockEndpoint mockA;
    
    @EndpointInject(uri = "mock:b", context = "camelContext")
    protected MockEndpoint mockB;
    
    @EndpointInject(uri = "mock:c", context = "camelContext2")
    protected MockEndpoint mockC;
    
    @Produce(uri = "direct:start", context = "camelContext")
    protected ProducerTemplate start;
    
    @Produce(uri = "direct:start2", context = "camelContext2")
    protected ProducerTemplate start2;
    
    @Test
    public void testPositive() throws Exception {
        assertEquals(ServiceStatus.Started, camelContext.getStatus());
        assertEquals(ServiceStatus.Started, camelContext2.getStatus());
        
        mockA.expectedBodiesReceived("David");
        mockB.expectedBodiesReceived("Hello David");
        mockC.expectedBodiesReceived("David");
        
        start.sendBody("David");
        start2.sendBody("David");
        
        MockEndpoint.assertIsSatisfied(camelContext);
    }
    
    @Test
    public void testJmx() throws Exception {
        assertEquals(DefaultManagementStrategy.class, camelContext.getManagementStrategy().getClass());
    }
    
    @Test
    public void testShutdownTimeout() throws Exception {
        assertEquals(10, camelContext.getShutdownStrategy().getTimeout());
        assertEquals(TimeUnit.SECONDS, camelContext.getShutdownStrategy().getTimeUnit());
    }
    
    @Test
    public void testStopwatch() {
        StopWatch stopWatch = StopWatchTestExecutionListener.getStopWatch();
        
        assertNotNull(stopWatch);
        assertTrue(stopWatch.taken() &lt; 100);
    }
    
    @Test
    public void testExcludedRoute() {
        assertNotNull(camelContext.getRoute("excludedRoute"));
    }
    
    @Test
    public void testProvidesBreakpoint() {
        assertNull(camelContext.getDebugger());
        assertNull(camelContext2.getDebugger());
    }

    @SuppressWarnings("deprecation")
    @Test
    public void testLazyLoadTypeConverters() {
        assertTrue(camelContext.isLazyLoadTypeConverters());
        assertTrue(camelContext2.isLazyLoadTypeConverters());
    }
}
]]></script>
</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. &nbsp;See&nbsp;<a href="/confluence/display/CAMEL/Spring+Testing"
title="Spring Testing">Spring Testing</a>&nbsp;for a list of annotations you
can use in your tests.</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">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
// to use camel-test-blueprint, then extend the CamelBlueprintTestSupport class,
// and add your unit tests methods as shown below.
public class DebugBlueprintTest extends CamelBlueprintTestSupport {

    private boolean debugBeforeMethodCalled;
    private boolean debugAfterMethodCalled;

    // override this method, and return the location of our Blueprint XML file to be used
for testing
    @Override
    protected String getBlueprintDescriptor() {
        return "org/apache/camel/test/blueprint/camelContext.xml";
    }
   
    // here we have regular JUnit @Test method
    @Test
    public void testRoute() throws Exception {

        // set mock expectations
        getMockEndpoint("mock:a").expectedMessageCount(1);

        // send a message
        template.sendBody("direct:start", "World");

        // assert mocks
        assertMockEndpointsSatisfied();

        // assert on the debugBefore/debugAfter methods below being called as we've enabled
the debugger
        assertTrue(debugBeforeMethodCalled);
        assertTrue(debugAfterMethodCalled);
    }

    @Override
    public boolean isUseDebugger() {
        // must enable debugger
        return true;
    }

    @Override
    protected void debugBefore(Exchange exchange, org.apache.camel.Processor processor, ProcessorDefinition&lt;?&gt;
definition, String id, String label) {
        log.info("Before " + definition + " with body " + exchange.getIn().getBody());
        debugBeforeMethodCalled = true;
    }

    @Override
    protected void debugAfter(Exchange exchange, org.apache.camel.Processor processor, ProcessorDefinition&lt;?&gt;
definition, String id, String label, long timeTaken) {
        log.info("After " + definition + " with body " + exchange.getIn().getBody());
        debugAfterMethodCalled = true;
    }
}
]]></script>
</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">
<script type="syntaxhighlighter" class="theme: Default; brush: xml; gutter: false"><![CDATA[
&lt;blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
             http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"&gt;

  &lt;camelContext xmlns="http://camel.apache.org/schema/blueprint"&gt;

    &lt;route&gt;
      &lt;from uri="direct:start"/&gt;
      &lt;transform&gt;
        &lt;simple&gt;Hello ${body}&lt;/simple&gt;
      &lt;/transform&gt;
      &lt;to uri="mock:a"/&gt;
    &lt;/route&gt;

  &lt;/camelContext&gt;

&lt;/blueprint&gt;
]]></script>
</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>
<tr>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/Stub" title="Stub">Stub</a>
</td>
<td class='confluenceTd'> Works like <a href="/confluence/display/CAMEL/SEDA" title="SEDA">SEDA</a>
but does not validate the endpoint uri, which makes stubbing much easier. </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;" class="grey">
                        <a href="https://cwiki.apache.org/confluence/users/removespacenotification.action?spaceKey=CAMEL">Stop
watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://cwiki.apache.org/confluence/users/editmyemailsettings.action">Change
email 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=30&originalVersion=29">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