camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject [CONF] Apache Camel > Mock
Date Fri, 13 Aug 2010 06:51:00 GMT
    <base href="">
            <link rel="stylesheet" href="/confluence/s/1810/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
<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="">Mock</a></h2>
    <h4>Page <b>edited</b> by             <a href="">Claus
                         <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" >There are some examples of the Mock
endpoint in use in the [camel-core processor tests|].
<br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">h3.
A Spring Example <br> <br>First, here&#39;s the [spring.xml file|]
<br> <br>{snippet:id=example|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/spring.xml}
<br> <br>As you can see, it defines a simple routing rule which consumes messages
from the local [src/test/data directory|].
The *noop* flag just means not to delete or move the file after its been processed. <br>
<br>Also note we instantiate a bean called *myBean*, here is the [source of the MyAssertions
 <br> <br>{snippet:id=example|lang=java|url=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/mock/}
<br> <br>The bean is injected with a bunch of Mock endpoints using the [@EndpointInject
annotation|Bean Integration], it then sets a bunch of expectations on startup (using Spring&#39;s
{{InitializingBean}} interface and {{afterPropertiesSet()}} method) before the {{CamelContext}}
starts up.  <br> <br>Then in our test case (which could be JUnit or TesNG) we
lookup *myBean* in Spring (or have it injected into our test) and then invoke the {{assertEndpointsValid()}}
method on it to verify that the mock endpoints have their assertions met. You could then inspect
the message exchanges that were delivered to any of the endpoints using the [getReceivedExchanges()|]
method on the Mock endpoint and perform further assertions or debug logging. <br> <br>Here
is the [actual JUnit test case we use|].
<br> <br></td></tr>
            <tr><td class="diff-unchanged" >{include:Endpoint See Also} <br>*
[Spring Testing] <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">*
[Testing] <br></td></tr>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Mock-MockComponent"></a>Mock Component</h2>

<p><a href="/confluence/display/CAMEL/Testing" title="Testing">Testing</a>
of distributed and asynchronous processing is notoriously difficult. The <a href="/confluence/display/CAMEL/Mock"
title="Mock">Mock</a>, <a href="/confluence/display/CAMEL/Test" title="Test">Test</a>
and <a href="/confluence/display/CAMEL/DataSet" title="DataSet">DataSet</a> endpoints
work great with the <a href="/confluence/display/CAMEL/Testing" title="Testing">Camel
Testing Framework</a> to simplify your unit and integration testing using <a href="/confluence/display/CAMEL/Enterprise+Integration+Patterns"
title="Enterprise Integration Patterns">Enterprise Integration Patterns</a> and Camel's
large range of <a href="/confluence/display/CAMEL/Components" title="Components">Components</a>
together with the powerful <a href="/confluence/display/CAMEL/Bean+Integration" title="Bean
Integration">Bean Integration</a>.</p>

<p>The Mock component provides a powerful declarative testing mechanism, which is similar
to <a href="" class="external-link" rel="nofollow">jMock</a> in
that it allows declarative expectations to be created on any Mock endpoint before a test begins.
Then the test is run, which typically fires messages to one or more endpoints, and finally
the expectations can be asserted in a test case to ensure the system  worked as expected.

<p>This allows you to test various things like:</p>
	<li>The correct number of messages are received on each endpoint,</li>
	<li>The correct payloads are received, in the right order,</li>
	<li>Messages arrive on an endpoint in order, using some <a href="/confluence/display/CAMEL/Expression"
title="Expression">Expression</a> to create an order testing function,</li>
	<li>Messages arrive match some kind of <a href="/confluence/display/CAMEL/Predicate"
title="Predicate">Predicate</a> such as that specific headers have certain values,
or that parts of the messages match some predicate, such as by evaluating an <a href="/confluence/display/CAMEL/XPath"
title="XPath">XPath</a> or <a href="/confluence/display/CAMEL/XQuery" title="XQuery">XQuery</a>
<a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a>.</li>

<p><b>Note</b> that there is also the <a href="/confluence/display/CAMEL/Test"
title="Test">Test endpoint</a> which is a Mock endpoint, but which uses a second
endpoint to provide the list of expected message bodies and automatically sets up the Mock
endpoint assertions. In other words, it's a Mock endpoint that automatically sets up its assertions
from some sample messages in a <a href="/confluence/display/CAMEL/File" title="File">File</a>
or <a href="/confluence/display/CAMEL/JPA" title="JPA">database</a>, for example.</p>

<h3><a name="Mock-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">

<p>Where <b>someName</b> can be any string that uniquely identifies the

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<h3><a name="Mock-Options"></a>Options</h3>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
<td class='confluenceTd'> <tt>reportGroup</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> A size to use a <a href="/confluence/display/CAMEL/Log"
title="Log">throughput logger</a> for reporting </td>

<h3><a name="Mock-SimpleExample"></a>Simple Example</h3>

<p>Here's a simple example of Mock endpoint in use. First, the endpoint is resolved
on the context. Then we set an expectation, and then, after the test has run, we assert that
our expectations have been met.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
MockEndpoint resultEndpoint = context.resolveEndpoint(<span class="code-quote">"mock:foo"</span>,


<span class="code-comment">// send some messages

<span class="code-comment">// now lets <span class="code-keyword">assert</span>
that the mock:foo endpoint received 2 messages

<p>You typically always call the <a href=""
class="external-link" rel="nofollow">assertIsSatisfied() method</a> to test that
the expectations were met after running a test.</p>

<p>Camel will by default wait 20 seconds when the <tt>assertIsSatisfied()</tt>
is invoked. This can be configured by setting the <tt>setResultWaitTime(millis)</tt>

<h3><a name="Mock-Settingexpectations"></a>Setting expectations</h3>

<p>You can see from the javadoc of <a href=""
class="external-link" rel="nofollow">MockEndpoint</a> the various helper methods
you can use to set expectations. The main methods are as follows: </p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<th class='confluenceTh'> Method </th>
<th class='confluenceTh'> Description </th>
<td class='confluenceTd'> <a href=""
class="external-link" rel="nofollow">expectedMessageCount(int)</a> </td>
<td class='confluenceTd'> To define the expected message count on the endpoint. </td>
<td class='confluenceTd'> <a href=""
class="external-link" rel="nofollow">expectedMinimumMessageCount(int)</a> </td>
<td class='confluenceTd'> To define the minimum number of expected messages on the endpoint.
<td class='confluenceTd'> <a href=""
class="external-link" rel="nofollow">expectedBodiesReceived(...)</a> </td>
<td class='confluenceTd'> To define the expected bodies that should be received (in
order). </td>
<td class='confluenceTd'> <a href=",%20java.lang.String)"
class="external-link" rel="nofollow">expectedHeaderReceived(...)</a> </td>
<td class='confluenceTd'> To define the expected header that should be received </td>
<td class='confluenceTd'> <a href=""
class="external-link" rel="nofollow">expectsAscending(Expression)</a> </td>
<td class='confluenceTd'> To add an expectation that messages are received in order,
using the given <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a>
to compare messages. </td>
<td class='confluenceTd'> <a href=""
class="external-link" rel="nofollow">expectsDescending(Expression)</a> </td>
<td class='confluenceTd'> To add an expectation that messages are received in order,
using the given <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a>
to compare messages. </td>
<td class='confluenceTd'> <a href=""
class="external-link" rel="nofollow">expectsNoDuplicates(Expression)</a> </td>
<td class='confluenceTd'> To add an expectation that no duplicate messages are received;
using an <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a>
to calculate a unique identifier for each message. This could be something like the <tt>JMSMessageID</tt>
if using JMS, or some unique reference number within the message. </td>

<p>Here's another example:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
resultEndpoint.expectedBodiesReceived(<span class="code-quote">"firstMessageBody"</span>,
<span class="code-quote">"secondMessageBody"</span>, <span class="code-quote">"thirdMessageBody"</span>);

<h4><a name="Mock-Addingexpectationstospecificmessages"></a>Adding expectations
to specific messages</h4>

<p>In addition, you can use the <a href=""
class="external-link" rel="nofollow">message(int messageIndex)</a> method to add
assertions about a specific message that is received.</p>

<p>For example, to add expectations of the headers or body of the first message (using
zero-based indexing like <tt>java.util.List</tt>), you can use the following code:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
resultEndpoint.message(0).header(<span class="code-quote">"foo"</span>).isEqualTo(<span

<p>There are some examples of the Mock endpoint in use in the <a href=""
class="external-link" rel="nofollow">camel-core processor tests</a>.</p>

<h3><a name="Mock-SeeAlso"></a>See Also</h3>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting

	<li><a href="/confluence/display/CAMEL/Spring+Testing" title="Spring Testing">Spring
	<li><a href="/confluence/display/CAMEL/Testing" title="Testing">Testing</a></li>

        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href=""
class="grey">Change Notification Preferences</a>
        <a href="">View Online</a>
        <a href="">View
        <a href=";showCommentArea=true#addcomment">Add

View raw message