camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Mock
Date Fri, 13 Aug 2010 06:51:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/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/Mock">Mock</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</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" >There are some examples of the Mock
endpoint in use in the [camel-core processor tests|http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/].
<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|http://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/spring.xml]
<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|http://svn.apache.org/repos/asf/acamel/trunk/components/camel-spring/src/test/data/].
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
bean|http://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/mock/MyAssertions.java].
 <br> <br>{snippet:id=example|lang=java|url=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/mock/MyAssertions.java}
<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()|http://activemq.apache.org/camel/maven/camel-core/apidocs/org/apache/camel/component/mock/MockEndpoint.html#getReceivedExchanges()]
method on the Mock endpoint and perform further assertions or debug logging. <br> <br>Here
is the [actual JUnit test case we use|http://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/mock/BeanMockTest.java].
<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>
        </table>
</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="http://jmock.org" 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>

<p>This allows you to test various things like:</p>
<ul>
	<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>
</ul>


<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">
mock:someName[?options]
</pre>
</div></div>

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

<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>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<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>
</tr>
</tbody></table>
</div>


<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>,
MockEndpoint.class);

resultEndpoint.expectedMessageCount(2);

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

<span class="code-comment">// now lets <span class="code-keyword">assert</span>
that the mock:foo endpoint received 2 messages
</span>resultEndpoint.assertIsSatisfied();
</pre>
</div></div>

<p>You typically always call the <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/component/mock/MockEndpoint.html#assertIsSatisfied()"
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>
method.</p>

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

<p>You can see from the javadoc of <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/component/mock/MockEndpoint.html"
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>
<tr>
<th class='confluenceTh'> Method </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/component/mock/MockEndpoint.html#expectedMessageCount(int)"
class="external-link" rel="nofollow">expectedMessageCount(int)</a> </td>
<td class='confluenceTd'> To define the expected message count on the endpoint. </td>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/component/mock/MockEndpoint.html#expectedMinimumMessageCount(int)"
class="external-link" rel="nofollow">expectedMinimumMessageCount(int)</a> </td>
<td class='confluenceTd'> To define the minimum number of expected messages on the endpoint.
</td>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/component/mock/MockEndpoint.html#expectedBodiesReceived(java.lang.Object...)"
class="external-link" rel="nofollow">expectedBodiesReceived(...)</a> </td>
<td class='confluenceTd'> To define the expected bodies that should be received (in
order). </td>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/component/mock/MockEndpoint.html#expectedHeaderReceived(java.lang.String,%20java.lang.String)"
class="external-link" rel="nofollow">expectedHeaderReceived(...)</a> </td>
<td class='confluenceTd'> To define the expected header that should be received </td>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/component/mock/MockEndpoint.html#expectsAscending(org.apache.camel.Expression)"
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>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/component/mock/MockEndpoint.html#expectsDescending(org.apache.camel.Expression)"
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>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/component/mock/MockEndpoint.html#expectsNoDuplicates(org.apache.camel.Expression)"
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>
</tr>
</tbody></table>
</div>


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

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

<p>In addition, you can use the <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/component/mock/MockEndpoint.html#message(int)"
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
class="code-quote">"bar"</span>);
</pre>
</div></div>

<p>There are some examples of the Mock endpoint in use in the <a href="http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/"
class="external-link" rel="nofollow">camel-core processor tests</a>.</p>

<h3><a name="Mock-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<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
Started</a></li>
</ul>

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

    </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/Mock">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=52081&revisedVersion=34&originalVersion=33">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Mock?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message