<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jayway Team Blog &#187; test</title>
	<atom:link href="http://blog.jayway.com/tag/test/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jayway.com</link>
	<description>Sharing Experience</description>
	<lastBuildDate>Sat, 11 Feb 2012 10:33:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Facebook Test Java API release 1.1.5</title>
		<link>http://blog.jayway.com/2011/10/20/facebook-test-java-api-release-1-1-5/</link>
		<comments>http://blog.jayway.com/2011/10/20/facebook-test-java-api-release-1-1-5/#comments</comments>
		<pubDate>Thu, 20 Oct 2011 12:23:38 +0000</pubDate>
		<dc:creator>Tobias Södergren</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=10597</guid>
		<description><![CDATA[The Facebook Test Java API framework has been updated. There are two additions to the API and one bugfix: Bugfix for NPE when calling facebookStore.createTestUser(false,"..."). Added possibility of using a provided HttpClient instance in HttpClientFacebookTestUserStore. Added unit test that displays the use of json-path when asserting data. Here's a quick reminder of how to create [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://code.google.com/p/facebook-test-java-api/" title="Facebook Test Java API">Facebook Test Java API</a> framework has been updated. There are two additions to the API and one bugfix:</p>
<ol>
<li>Bugfix for NPE when calling facebookStore.createTestUser(false,"...").</li>
<li>Added possibility of using a provided HttpClient instance in HttpClientFacebookTestUserStore.</li>
<li>Added unit test that displays the use of json-path when asserting data.</li>
</ol>
<p>Here's a quick reminder of how to create test users for an application. The creation process is handled by an instance of FacebookTestUserStore, e.g. The HttpClientFacebookTestUserStore which uses Apache HttpClient as communication method. The class needs the <i>application ID</i> and the <i>application secret</i> for the application in order to be able to create the users and this is specified in the constructor:</p>
<pre class="brush:java">
FacebookTestUserStore facebookStore = new HttpClientFacebookTestUserStore("&lt;appId&gt;", "&lt;appSecret&gt;"));
</pre>
<p>Now you have a choice when creating a test user: You may say that the test user should accept the privileges that you specify for it, which is done by:</p>
<pre class="brush:java">
FacebookTestUserAccount aTestUser = facebookStore.createTestUser(true, "read_stream");
</pre>
<p>If you want to give a choice to the test user whether the privileges should be accepted or not, you do the following:</p>
<pre class="brush:java">
FacebookTestUserAccount aTestUser = facebookStore.createTestUser(false, "read_stream");
</pre>
<p>The accept/decline choice takes part in the web browser, hence you need the login URL for the test user to access the page. This URL is accessible from the FacebookTestUserAccount:</p>
<pre class="brush:java">
aTestUser.loginUrl();
</pre>
<h3>Specifying a HttpClient instance to HttpClientFacebookTestUserStore</h3>
<p>There might be occasions where you need a special HttpClient instance for communication, for example one instance with proxy settings configures, or another instance where you have stubbed all the communication that takes place. To provide the instance to HttpClientFacebookTestUserStore, just specify it in the constructor:</p>
<pre class="brush:java">
HttpClient httpClient = new DefaultHttpClient();
FacebookTestUserStore facebookStore = new HttpClientFacebookTestUserStore("&lt;appId&gt;", "&lt;appSecret&gt;", httpClient));
</pre>
<h3>Using json-assert to verify application behavior</h3>
<p>The <a href="http://code.google.com/p/json-assert/" title="json-assert">json-assert</a> project, which uses <a href="http://code.google.com/p/json-path/" title="json-path">json-path</a> can be used to make life a little less verbose when asserting on data from the JSON document representations of the Facebook data streams that are available in the FacebookTestUserAccount. For example, the user details of a test user can be accessed like this:</p>
<pre class="brush:java">
aTestUser.getUserDetails();
</pre>
<p>The result is a java.lang.String containing a JSON Document. To make it easier to assert that the user details actually contain the expected data, you may use json-assert and <a href="http://code.google.com/p/hamcrest/wiki/Tutorial" title="Hamcrest matchers">Hamcrest matchers</a>:</p>
<pre class="brush:java">
String userDetails = account.getUserDetails();

JsonAssert.with(userDetails).assertThat("$.first_name", equalTo("Sue"));
JsonAssert.with(userDetails).assertThat("$.last_name", equalTo("Ellen"));
</pre>
<p>Feel free to test it out, the code is open source an can be found on Maven central or on the<br />
<a href="http://code.google.com/p/facebook-test-java-api/" title="Facebook Test Java API project page">Facebook Test Java API project page</a>.</p>
<p>/Tobias</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/10/20/facebook-test-java-api-release-1-1-5/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Simple Parsing of Complex JSON and XML Documents in Java</title>
		<link>http://blog.jayway.com/2011/10/09/simple-parsing-of-complex-json-and-xml-documents-in-java/</link>
		<comments>http://blog.jayway.com/2011/10/09/simple-parsing-of-complex-json-and-xml-documents-in-java/#comments</comments>
		<pubDate>Sun, 09 Oct 2011 15:12:15 +0000</pubDate>
		<dc:creator>Johan Haleby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=9743</guid>
		<description><![CDATA[In this blog I'm going to demonstrate how to parse and validate more complex JSON and XML documents in Java using the REST Assured framework and the XmlPath and JsonPath components available in this framework. Since REST Assured is implemented in Groovy it can be really beneficial to take advantage of Groovy's collection API. Let's [...]]]></description>
			<content:encoded><![CDATA[<p>In this blog I'm going to demonstrate how to parse and validate more complex JSON and XML documents in Java using the <a href="http://code.google.com/p/rest-assured/">REST Assured</a> framework and the <a href="http://rest-assured.googlecode.com/svn/tags/1.4/apidocs/com/jayway/restassured/path/xml/XmlPath.html">XmlPath</a> and <a href="http://rest-assured.googlecode.com/svn/tags/1.4/apidocs/com/jayway/restassured/path/json/JsonPath.html">JsonPath</a> components available in this framework. Since REST Assured is implemented in Groovy it can be really beneficial to take advantage of Groovy's <a href="http://groovy.codehaus.org/Collections">collection</a> API. Let's begin by looking at an example in Groovy:</p>
<pre class="groovy">&nbsp;
<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> words = <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'ant'</span>, <span style="color: #ff0000;">'buffalo'</span>, <span style="color: #ff0000;">'cat'</span>, <span style="color: #ff0000;">'dinosaur'</span><span style="color: #66cc66;">&#93;</span>
<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> wordsWithSizeGreaterThanFour = words.<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20findAll"><span style="color: #663399;">findAll</span></a> <span style="color: #66cc66;">&#123;</span> it.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>At the first line we simply define a list with some words but the second line is more interesting. Here we search the <code>words</code> list for all words that are longer than 4 characters by calling the <code>findAll</code> with a <a href="http://groovy.codehaus.org/Closures">Groovy closure</a>. The closure has an implicit variable called "it" which represents the current item in the list. The result is a new list, <code>wordsWithSizeGreaterThanFour</code>, containing <code>buffalo</code> and <code>dinosaur</code>. Pretty nice and simple! There are other interesting methods that we can use on collections in Groovy as well, e.g.</p>
<ul>
<li>find - finds the first item matching a closure predicate</li>
<li>collect - collect the return value of calling a closure on each item in a collection</li>
<li>sum - Sum all the items in the collection</li>
<li>max/min - returns the max/min values of the collection</li>
</ul>
<p>So how do we take advantage of this when validating our XML or JSON responses with REST Assured?</p>
<h2>XML Example</h2>
<p>Let's say we have a resource at "localhost:8080/shopping" that returns the following XML:</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;shopping<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;category</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;groceries&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;item<span style="font-weight: bold; color: black;">&gt;</span></span></span>Chocolate<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/item<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;item<span style="font-weight: bold; color: black;">&gt;</span></span></span>Coffee<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/item<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/category<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;category</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;supplies&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;item<span style="font-weight: bold; color: black;">&gt;</span></span></span>Paper<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/item<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;item</span> <span style="color: #000066;">quantity</span>=<span style="color: #ff0000;">&quot;4&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>Pens<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/item<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/category<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;category</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;present&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;item</span> <span style="color: #000066;">when</span>=<span style="color: #ff0000;">&quot;Aug 10&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>Kathryn's Birthday<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/item<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/category<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/shopping<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<p>Let's also say we want to write a test that verifies that the category of type <code>groceries</code> has items Chocolate and Coffee. In REST Assured it can look like this:</p>
<pre class="java">&nbsp;
expect<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;shopping.category.find { it.@type == 'groceries' }.item&quot;</span>, hasItems<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Chocolate&quot;</span>, <span style="color: #ff0000;">&quot;Coffee&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">when</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/shopping&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre>
</pre>
<p>What's going on here? First of all the XML path "shopping.category" returns a list of all categories. On this list we invoke a function, <code>find</code>, to return the single category that has the XML attribute, <code>type</code>, equal to "groceries". On this category we then continue by getting all the items associated with this category. Since there are more than one item associated with the groceries category a list will be returned and we verify this list against the hasItems <a href="http://code.google.com/p/hamcrest/">Hamcrest</a> matcher. </p>
<p>But what if you want to get the items and not validate them against a Hamcrest matcher? This is also simple using the <a href="http://rest-assured.googlecode.com/svn/tags/1.4/apidocs/com/jayway/restassured/path/xml/XmlPath.html">XmlPath</a> class:</p>
<pre class="java">&nbsp;
<span style="color: #808080; font-style: italic;">// Get the response body as a String</span>
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> response = get<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/shopping&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">asString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// And get the grocieries from the response. &quot;from&quot; is statically imported from the XmlPath class</span>
List&lt;String&gt; groceries = from<span style="color: #66cc66;">&#40;</span>response<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getList</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;shopping.category.find { it.@type == 'groceries' }.item&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>If the list of groceries is the only thing you care about in the response body you can also use a shortcut:</p>
<pre class="java">&nbsp;
<span style="color: #808080; font-style: italic;">// Get the response body as a String</span>
List&lt;String&gt; groceries = get<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/shopping&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">path</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;shopping.category.find { it.@type == 'groceries' }.item&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<h3>Depth-first search</h3>
<p>It's actually possible to simplify the previous example even further:</p>
<pre class="java">&nbsp;
expect<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;**.find { it.@type == 'groceries' }&quot;</span>, hasItems<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Chocolate&quot;</span>, <span style="color: #ff0000;">&quot;Coffee&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">when</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/shopping&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p><code>**</code> is a shortcut for doing depth first searching in the XML document. We search for the first node that has an attribute named <code>type</code> equal to "groceries". Notice also that we don't end the XML path with "item". The reason is that <code>toString()</code> is called automatically on the category node which returns a list of the item values.</p>
<h3>Root path</h3>
<p>Let's expand the example by also validating that the category of type supplies contains Paper and Pens:</p>
<pre class="java">&nbsp;
expect<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;**.find { it.@type == 'groceries' }&quot;</span>, hasItems<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Chocolate&quot;</span>, <span style="color: #ff0000;">&quot;Coffee&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;**.find { it.@type == 'supplies' }&quot;</span>, hasItems<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Paper&quot;</span>, <span style="color: #ff0000;">&quot;Pens&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">when</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/shopping&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Even though this works you can probably see that the XML path is almost duplicated for the two body expectations and this is of course something we want to avoid. To address this we can use a root path and path arguments:</p>
<pre class="java">&nbsp;
expect<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">rootPath</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;**.find { it.@type == '%s' }&quot;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span>, withArgs<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;groceries&quot;</span><span style="color: #66cc66;">&#41;</span>, hasItems<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Chocolate&quot;</span>, <span style="color: #ff0000;">&quot;Coffee&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span>, withArgs<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;supplies&quot;</span><span style="color: #66cc66;">&#41;</span>, hasItems<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Paper&quot;</span>, <span style="color: #ff0000;">&quot;Pens&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">when</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/shopping&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>By specifiying a "root path" we don't need to duplicate the entire path just to change the category type.</p>
<h2>JSON Example</h2>
<p>Let's say we have a resource at "/store" that returns the following JSON document:</p>
<pre>&nbsp;
{ &quot;store&quot; : {
  &quot;book&quot; : [
          { &quot;author&quot; : &quot;Nigel Rees&quot;,
            &quot;category&quot; : &quot;reference&quot;,
            &quot;price&quot; : 8.95,
            &quot;title&quot; : &quot;Sayings of the Century&quot;
          },
          { &quot;author&quot; : &quot;Evelyn Waugh&quot;,
            &quot;category&quot; : &quot;fiction&quot;,
            &quot;price&quot; : 12.99,
            &quot;title&quot; : &quot;Sword of Honour&quot;
          },
          { &quot;author&quot; : &quot;Herman Melville&quot;,
            &quot;category&quot; : &quot;fiction&quot;,
            &quot;isbn&quot; : &quot;0-553-21311-3&quot;,
            &quot;price&quot; : 8.99,
            &quot;title&quot; : &quot;Moby Dick&quot;
          },
          { &quot;author&quot; : &quot;J. R. R. Tolkien&quot;,
            &quot;category&quot; : &quot;fiction&quot;,
            &quot;isbn&quot; : &quot;0-395-19395-8&quot;,
            &quot;price&quot; : 22.99,
            &quot;title&quot; : &quot;The Lord of the Rings&quot;
          }
        ]
   }
}
&nbsp;</pre>
<h3>Example 1</h3>
<p>As a first example let's say we want to make the request to "/store" and assert that the titles of the books with a price less than 10 are "Sayings of the Century" and "Moby Dick":</p>
<pre class="java">&nbsp;
expect<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
        <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;store.book.findAll { it.price &lt; 10 }.title&quot;</span>, hasItems<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Sayings of the Century&quot;</span>, <span style="color: #ff0000;">&quot;Moby Dick&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">when</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
        <span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/store&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Just as in the XML examples above we use a closure to find all books with a price less than 10 and then return the titles of all the books. We then use the <code>hasItems</code> matcher to assert that the titles are the ones we expect. Using <a href="http://rest-assured.googlecode.com/svn/tags/1.4/apidocs/com/jayway/restassured/path/json/JsonPath.html">JsonPath</a> we can return the titles instead:</p>
<pre class="java">&nbsp;
<span style="color: #808080; font-style: italic;">// Get the response body as a String</span>
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> response = get<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/store&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">asString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// And get all books with price &lt; 10 from the response. &quot;from&quot; is statically imported from the JsonPath class</span>
List&lt;String&gt; bookTitles = from<span style="color: #66cc66;">&#40;</span>response<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getList</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;store.book.findAll { it.price &lt; 10 }.title&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<h3>Example 2</h3>
<p>Let's consider instead that we want to assert that the sum of the length of all author names are greater than 50. This is a rather complex question to answer and it really shows the strength of closures and Groovy collections. In REST Assured it looks like this:</p>
<pre class="java">&nbsp;
expect<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
        <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;store.book.author.collect { it.length() }.sum()&quot;</span>, greaterThan<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">when</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
        <span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/store&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>First we get all the authors ("store.book.author") and invoke the <code>collect</code> method on the resulting list with the closure <code>{ it.length() }</code>. What it does is to call the <code>length()</code> method on each author in the list and returns the result to a new list. On this list we simply call the <code>sum()</code> method to sum all the length's. The end result is <code>53</code> and we assert that it's greater than 50 by using the <code>greaterThan</code> matcher. But it's actually possible to simplify this even further. Consider the <code>words</code> example from the beginning of the article again:</p>
<pre class="groovy">&nbsp;
<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> words = <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'ant'</span>, <span style="color: #ff0000;">'buffalo'</span>, <span style="color: #ff0000;">'cat'</span>, <span style="color: #ff0000;">'dinosaur'</span><span style="color: #66cc66;">&#93;</span>
&nbsp;</pre>
<p>Groovy has a very handy way of calling a function for each element in the list by using the <code>*</code> notation, e.g.</p>
<pre class="groovy">&nbsp;
<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> words = <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'ant'</span>, <span style="color: #ff0000;">'buffalo'</span>, <span style="color: #ff0000;">'cat'</span>, <span style="color: #ff0000;">'dinosaur'</span><span style="color: #66cc66;">&#93;</span>
<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20assert"><span style="color: #000000; font-weight: bold;">assert</span></a> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#93;</span> == words*.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;</pre>
<p>I.e. Groovy returns a new list with the lengths of the items in the words list. We can utilize this for the author list in REST Assured as well:</p>
<pre class="java">&nbsp;
expect<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
        <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;store.book.author*.length().sum()&quot;</span>, greaterThan<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">when</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
        <span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/store&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>And of course we can use <a href="http://rest-assured.googlecode.com/svn/tags/1.4/apidocs/com/jayway/restassured/path/json/JsonPath.html">JsonPath</a> to actually return the result:</p>
<pre class="java">&nbsp;
<span style="color: #808080; font-style: italic;">// Get the response body as an input stream</span>
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AInputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">InputStream</span></a> response = get<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/store&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">asInputStream</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// Get the sum of all author length's as an int. &quot;from&quot; is again statically imported from the JsonPath class</span>
<span style="color: #993333;">int</span> sumOfAllAuthorLengths = from<span style="color: #66cc66;">&#40;</span>response<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getInt</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;store.book.author*.length().sum()&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// We can also assert that the sum is equal to 53 as expected.</span>
assertThat<span style="color: #66cc66;">&#40;</span>sumOfAllAuthorLengths, is<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">53</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<h2>Summary</h2>
<p>At a first glance the syntax may seem quite strange and complex but once you get used to it it's extremely powerful and actually quite simple. It's also a good place to start if you work with Java and want to understand the benefit of closures. There are a lot more features that can help you parsing and validating JSON and XML using <a href="http://code.google.com/p/rest-assured/">REST Assured</a> than the ones shown in this article. For further reading refer to the <a href="http://code.google.com/p/rest-assured/">REST Assured web-page</a> and read up on <a href="http://groovy.codehaus.org/Collections">Groovy collections</a>. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/10/09/simple-parsing-of-complex-json-and-xml-documents-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is your REST assured?</title>
		<link>http://blog.jayway.com/2011/06/04/is-your-rest-assured/</link>
		<comments>http://blog.jayway.com/2011/06/04/is-your-rest-assured/#comments</comments>
		<pubDate>Sat, 04 Jun 2011 08:01:33 +0000</pubDate>
		<dc:creator>Johan Haleby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[automated testing]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=8558</guid>
		<description><![CDATA[A couple of months ago I introduced a new open source Java framework for simple automated testing of REST/HTTP services called REST Assured. A lot of things have happended to the framework since then and it's now better than ever. In this blog I'm going to summarize some of the most important new features that [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of months ago I <a href="http://blog.jayway.com/2010/12/27/rest-assured-or-how-to-easily-test-rest-services-in-java/">introduced</a> a new open source Java framework for simple automated testing of REST/HTTP services called <a href="http://code.google.com/p/rest-assured/">REST Assured</a>. A lot of things have happended to the framework since then and it's now better than ever. In this blog I'm going to summarize some of the most important new features that have been added since the last blog post.</p>
<h3>Easy parsing of the response body</h3>
<h4>JSON</h4>
<p>Let's assume that a REST service at <code>localhost:8080/json</code> returns:</p>
<pre class="javascript">&nbsp;
<span style="color: #66cc66;">&#123;</span>
<span style="color: #3366CC;">&quot;lotto&quot;</span>:<span style="color: #66cc66;">&#123;</span>
 <span style="color: #3366CC;">&quot;lottoId&quot;</span>:<span style="color: #CC0000;">5</span>,
 <span style="color: #3366CC;">&quot;winning-numbers&quot;</span>:<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">2</span>,<span style="color: #CC0000;">45</span>,<span style="color: #CC0000;">34</span>,<span style="color: #CC0000;">23</span>,<span style="color: #CC0000;">7</span>,<span style="color: #CC0000;">5</span>,<span style="color: #CC0000;">3</span><span style="color: #66cc66;">&#93;</span>,
 <span style="color: #3366CC;">&quot;winners&quot;</span>:<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#123;</span>
   <span style="color: #3366CC;">&quot;winnerId&quot;</span>:<span style="color: #CC0000;">23</span>,
   <span style="color: #3366CC;">&quot;numbers&quot;</span>:<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">2</span>,<span style="color: #CC0000;">45</span>,<span style="color: #CC0000;">34</span>,<span style="color: #CC0000;">23</span>,<span style="color: #CC0000;">3</span>,<span style="color: #CC0000;">5</span><span style="color: #66cc66;">&#93;</span>
 <span style="color: #66cc66;">&#125;</span>,<span style="color: #66cc66;">&#123;</span>
   <span style="color: #3366CC;">&quot;winnerId&quot;</span>:<span style="color: #CC0000;">54</span>,
   <span style="color: #3366CC;">&quot;numbers&quot;</span>:<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">52</span>,<span style="color: #CC0000;">3</span>,<span style="color: #CC0000;">12</span>,<span style="color: #CC0000;">11</span>,<span style="color: #CC0000;">18</span>,<span style="color: #CC0000;">22</span><span style="color: #66cc66;">&#93;</span>
 <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>As a recap let's say you want to validate that the lottoId is equal to 5 and winnerIds are 23 and 54 you can do like this:</p>
<pre class="java">&nbsp;
expect<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;lotto.lottoId&quot;</span>, equalTo<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;lotto.winners.winnderId&quot;</span>, hasItems<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">23</span>, <span style="color: #cc66cc;">54</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">when</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
       <span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/lotto&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>where "equalTo" and "hasItems" are standard <a href="http://code.google.com/p/hamcrest/">Hamcrest</a> matchers. But what if you want to return the lottoId or winnder id's and do something with the values? For this reason <a href="http://rest-assured.googlecode.com/svn/tags/1.2.1/apidocs/com/jayway/restassured/path/json/JsonPath.html">JsonPath</a> has been added to REST Assured. Example:</p>
<pre class="java">&nbsp;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> response = get<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/lotto&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">asString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Assuming we've statically imported the &quot;from&quot; method in JsonPath we can do:</span>
<span style="color: #993333;">int</span> lottoId = from<span style="color: #66cc66;">&#40;</span>response<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;lotto.lottoId&quot;</span><span style="color: #66cc66;">&#41;</span>;
List&lt;Integer&gt; winnerIds = from<span style="color: #66cc66;">&#40;</span>response<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;lotto.winners.winnderId&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
..
&nbsp;</pre>
<p>You can also set a so called "root path" so that you don't have to repeat the entire path for every "get":</p>
<pre class="java">&nbsp;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> responseString = get<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/lotto&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">asString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
JsonPath response = <span style="color: #000000; font-weight: bold;">new</span> JsonPath<span style="color: #66cc66;">&#40;</span>responseString<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">setRoot</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;lotto&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333;">int</span> lottoId = response.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;lottoId&quot;</span><span style="color: #66cc66;">&#41;</span>;
List&lt;Integer&gt; winnerIds = response.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;winners.winnderId&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<h3>XML</h3>
<p>The equilvant of JsonPath for XML is called <a href="http://rest-assured.googlecode.com/svn/tags/1.2.1/apidocs/com/jayway/restassured/path/xml/XmlPath.html">XmlPath</a> and it works in very much the same way as JsonPath but arguably a bit more complex because of the nature of XML. Consider the following XML available at <code>localhost:8080/xml</code>:</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;records<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;car</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">'HSV Maloo'</span> <span style="color: #000066;">make</span>=<span style="color: #ff0000;">'Holden'</span> <span style="color: #000066;">year</span>=<span style="color: #ff0000;">'2006'</span><span style="font-weight: bold; color: black;">&gt;</span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;country<span style="font-weight: bold; color: black;">&gt;</span></span></span>Australia<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/country<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;record</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">'speed'</span><span style="font-weight: bold; color: black;">&gt;</span></span>Pickup Truck with speed of 271kph<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/record<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/car<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;car</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">'P50'</span> <span style="color: #000066;">make</span>=<span style="color: #ff0000;">'Peel'</span> <span style="color: #000066;">year</span>=<span style="color: #ff0000;">'1962'</span><span style="font-weight: bold; color: black;">&gt;</span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;country<span style="font-weight: bold; color: black;">&gt;</span></span></span>Isle of Man<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/country<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;record</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">'size'</span><span style="font-weight: bold; color: black;">&gt;</span></span>Street-Legal Car at 99cm wide, 59kg<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/record<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/car<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/records<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<p>Now let's make a request and parse the XML:</p>
<pre class="java">&nbsp;
<span style="color: #808080; font-style: italic;">// Get the XML from localhost:8080/xml</span>
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> xml = get<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/xml&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">asString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Get the country of the first car (“from” is statically imported from from XmlPath)</span>
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> country = from<span style="color: #66cc66;">&#40;</span>xml<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;records.car[0].country”);
&nbsp;
// Get a list of all car names
List&lt;String&gt; carNames = from(xml).get(&quot;</span>records.<span style="color: #006600;">car</span>.@name<span style="color: #ff0000;">&quot;);
</span></pre>
<p>As with JsonPath you can also set a root path to make it a bit more efficient:</p>
<pre class="java">&nbsp;
XmlPath xmlPath = <span style="color: #000000; font-weight: bold;">new</span> XmlPath<span style="color: #66cc66;">&#40;</span>xml<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">setRoot</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;records&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> country = xmlPath.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;car[0].country&quot;</span><span style="color: #66cc66;">&#41;</span>;
List&lt;String&gt; carNames = xmlPath.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;car.@name&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<h2>Filters</h2>
<p>Another powerful feature is support for filters. A filter allows you to inspect and alter a request before it's actually committed and also inspect and alter the response before it's returned to the expectations. You can regard it as an "around advice" in AOP terms. Filters can be used to implement custom authentication schemes, session management, logging etc. To create a filter you need to implement the com.jayway.restassured.filter.log.Filter interface. Here's an example filter that simply logs the response body to the console:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SystemOutFilter <span style="color: #000000; font-weight: bold;">implements</span> Filter <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Response filter<span style="color: #66cc66;">&#40;</span>FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> Response response = ctx.<span style="color: #006600;">next</span><span style="color: #66cc66;">&#40;</span>requestSpec, responseSpec<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Invoke the request by delegating to the next filter in the filter chain.</span>
        <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span>response.<span style="color: #006600;">asString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000000; font-weight: bold;">return</span> response;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>You can then apply your filter like this:</p>
<pre class="java">&nbsp;
given<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">filter</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SystemOutFilter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>. ..
&nbsp;</pre>
<p>REST Assured already comes with two pre-defined logging filters called <a href="http://rest-assured.googlecode.com/svn/tags/1.2.1/apidocs/com/jayway/restassured/filter/log/ResponseLoggingFilter.html">ResponseLoggingFilter</a> and <a href="http://rest-assured.googlecode.com/svn/tags/1.2.1/apidocs/com/jayway/restassured/filter/log/ErrorLoggingFilter.html">ErrorLoggingFilter</a> are there's also shortcuts for using these filters (see below).</p>
<h2>Logging</h2>
<p>When writing a test it's often useful to see what the response body actually looks like. To easily accomodate this REST Assured provides a shortcut for adding the two logging filters introduced in the previous section. So to log the response body regardless of the status code you can just do:</p>
<pre class="java">&nbsp;
expect<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;lotto.lottoId&quot;</span>, equalTo<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;lotto.winners.winnderId&quot;</span>, hasItems<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">23</span>, <span style="color: #cc66cc;">54</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">when</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/lotto&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>If you only which to log when an error occurs you can do:</p>
<pre class="java">&nbsp;
expect<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">logOnError</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;lotto.lottoId&quot;</span>, equalTo<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;lotto.winners.winnderId&quot;</span>, hasItems<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">23</span>, <span style="color: #cc66cc;">54</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">when</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/lotto&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<h2>Form authentication</h2>
<p>Form authentication is one of the most popular authentication techniques on the net today and using REST Assured it's very simple to test services requiring this scheme. Imagine that the request to <code>localhost:8080/json</code> requires form authentication, we can then do:</p>
<pre class="java">&nbsp;
given<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">auth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">form</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;username&quot;</span>, <span style="color: #ff0000;">&quot;password&quot;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">expect</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;lotto.lottoId&quot;</span>, equalTo<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;lotto.winners.winnderId&quot;</span>, hasItems<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">23</span>, <span style="color: #cc66cc;">54</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">when</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/lotto&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>In many cases it's as simple as that! Basically what happens is that REST Assured parses the login page and logs in for you before making the original request. But if you have many tests or if REST Assured is not able to parse the login page you can supply something called a FormAuthConfig to tell REST Assured how to login. What you provide is the form action and the user name and password input tags. This option will also make your test run faster since there's no need for REST Assured to make an additional requests and parse the login page. For example:</p>
<pre class="java">&nbsp;
given<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">auth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">form</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;username&quot;</span>, <span style="color: #ff0000;">&quot;password&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> FilterConfig<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/action&quot;</span>, <span style="color: #ff0000;">&quot;usernameField&quot;</span>, <span style="color: #ff0000;">&quot;passwordField&quot;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">expect</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">logOnError</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;lotto.lottoId&quot;</span>, equalTo<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;lotto.winners.winnderId&quot;</span>, hasItems<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">23</span>, <span style="color: #cc66cc;">54</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">when</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
         <span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/lotto&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>If you're using Spring Security there's a pre-defined FilterAuthConfig:</p>
<pre class="java">&nbsp;
given<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">auth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">form</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;username&quot;</span>, <span style="color: #ff0000;">&quot;password&quot;</span>, FilterConfig.<span style="color: #006600;">springSecurity</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
&nbsp;</pre>
<h2>Reusable specifications</h2>
<p>Instead of having to duplicate response expectations and/or request parameters for different tests you can re-use an entire specification. To do this you define a specification using either the RequestSpecBuilder or ResponseSpecBuilder.</p>
<p>E.g. let's say you want to make sure that the expected status code is 200 and that JSON the size of the JSON array "x.y" has size 2 in several tests you can define a ResponseSpecBuilder like this:</p>
<pre class="java">&nbsp;
ResponseSpecBuilder builder = <span style="color: #000000; font-weight: bold;">new</span> ResponseSpecBuilder<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
builder.<span style="color: #006600;">expectStatusCode</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>;
builder.<span style="color: #006600;">expectBody</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;x.y.size()&quot;</span>, is<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
ResponseSpecification responseSpec = builder.<span style="color: #006600;">build</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Now you can re-use the &quot;responseSpec&quot; in many different tests:</span>
expect<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
       <span style="color: #006600;">spec</span><span style="color: #66cc66;">&#40;</span>responseSpec<span style="color: #66cc66;">&#41;</span>.
       <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;x.y.z&quot;</span>, equalTo<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;something&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">when</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
       <span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/something&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>In this example the data defined in "responseSpec" is merged with the additional body expectation and all expectations must be fulfilled in order for the test to pass.</p>
<p>You can do the same thing if you need to re-use request data in different tests. E.g.</p>
<pre class="java">&nbsp;
RequestSpecBuilder builder = <span style="color: #000000; font-weight: bold;">new</span> RequestSpecBuilder<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
builder.<span style="color: #006600;">addParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;parameter1&quot;</span>, <span style="color: #ff0000;">&quot;parameterValue&quot;</span><span style="color: #66cc66;">&#41;</span>;
builder.<span style="color: #006600;">addHeader</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;header1&quot;</span>, <span style="color: #ff0000;">&quot;headerValue&quot;</span><span style="color: #66cc66;">&#41;</span>;
RequestSpecification requestSpec = builder.<span style="color: #006600;">build</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
given<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
        <span style="color: #006600;">spec</span><span style="color: #66cc66;">&#40;</span>requestSpec<span style="color: #66cc66;">&#41;</span>.
        <span style="color: #006600;">param</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;parameter2&quot;</span>, <span style="color: #ff0000;">&quot;paramValue&quot;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">expect</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
        <span style="color: #006600;">body</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;x.y.z&quot;</span>, equalTo<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;something&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.
<span style="color: #006600;">when</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.
        <span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/something&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Here the request's data is merged with the data in the "requestSpec" so the request will contain two parameters ("parameter1" and "parameter2") and one header ("header1").</p>
<h2>Summary</h2>
<p>As you've hopefully seen there are many new useful features added to REST Assured. Other new features that have not been mentioned in this post are e.g. improved HTML parsing, very simple XSD/DTD validation, multi-value parameters, setting root path on expectations, improved configuration of default settings etc etc. In the future we'll hope to add support for JSONP, better XML parsing using XmlPath and a more BDD-like (given, when, then) syntax. Please visit our <a href="http://code.google.com/p/rest-assured/">homepage</a> for more information and downloads.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/06/04/is-your-rest-assured/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Developing JMeter plugins using Maven</title>
		<link>http://blog.jayway.com/2010/07/07/developing-jmeter-plugins-using-maven/</link>
		<comments>http://blog.jayway.com/2010/07/07/developing-jmeter-plugins-using-maven/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 15:44:22 +0000</pubDate>
		<dc:creator>Jan Kronquist</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[jmeter]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=5814</guid>
		<description><![CDATA[I have been using JMeter for load testing and I needed to create a custom plugin to handle parsing JSON results but I couldn't find a simple guide how to do this. I wanted a simple solution that didn't require manual installation in my repository or a repository manager such as Nexus. ]]></description>
			<content:encoded><![CDATA[<p>I have been using <a href="http://jakarta.apache.org/jmeter/">JMeter</a> for load testing and I needed to create a custom plugin to handle parsing JSON results but I couldn't find a simple guide how to do this. I wanted a simple solution that didn't require manual installation in my repository or a repository manager such as <a href="http://nexus.sonatype.org/">Nexus</a>. </p>
<h3>Building the plugin</h3>
<p>Building a plugin using maven is easy, simply add the following to your pom:</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;properties<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;jmeter</span>.home<span style="font-weight: bold; color: black;">&gt;</span></span>installation directory of jmeter<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/jmeter</span>.home<span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/properties<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;dependencies<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>org.apache.jmeter<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>jmeter-core<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;version<span style="font-weight: bold; color: black;">&gt;</span></span></span>2.3.4<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/version<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;scope<span style="font-weight: bold; color: black;">&gt;</span></span></span>system<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/scope<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;systemPath<span style="font-weight: bold; color: black;">&gt;</span></span></span>${jmeter.home}/lib/ext/ApacheJMeter_core.jar<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/systemPath<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>org.apache.jmeter<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>jmeter-jorphan<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;version<span style="font-weight: bold; color: black;">&gt;</span></span></span>2.3.4<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/version<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;scope<span style="font-weight: bold; color: black;">&gt;</span></span></span>system<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/scope<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;systemPath<span style="font-weight: bold; color: black;">&gt;</span></span></span>${jmeter.home}/lib/jorphan.jar<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/systemPath<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/dependencies<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>You probably want to put <code>jmeter.home</code> in your <code>settings.xml</code> instead.</p>
<h3>Testing the plugin</h3>
<p>Whenever I develop something I want to have a quick build/test cycle. In this case I want to to start the JMeter UI without having to update the JMeter installation or any other manual work. I didn't get <a href="http://mojo.codehaus.org/chronos-maven-plugin/">chronos</a> to add my plugin to the UI. It seems chronos copied the jar files to <code>JMETER_HOME/lib/junit</code> and this probably works for running an already created test, but not for creating the test. I also failed to the the <a href="http://mojo.codehaus.org/exec-maven-plugin/index.html">maven exec plugin</a> to create the command line arguments I needed. Instead I added a small ant script to start JMeter:</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;build<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;plugins<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;plugin<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>org.apache.maven.plugins<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>maven-antrun-plugin<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;configuration<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;tasks<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;runtime_classpath&quot;</span>
           <span style="color: #000066;">refid</span>=<span style="color: #ff0000;">&quot;maven.runtime.classpath&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
          <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;exec</span> <span style="color: #000066;">executable</span>=<span style="color: #ff0000;">&quot;${jmeter.home}/bin/jmeter.bat&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;arg</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;-Jsearch_paths=${runtime_classpath}&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
          <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/exec<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/tasks<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/configuration<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/plugin<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/plugins<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/build<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>To run the jmeter gui simply run <code>mvn antrun:run</code>. This only works for Windows, I leave other platforms as an exercise to the reader <img src='http://blog.jayway.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> . </p>
<h3>Using the plugin</h3>
<p>I ended up copying the plugin jar and dependencies to the JMeter installation and used then this modified JMeter distribution when performing the tests. The reason was that I didn't have maven installed on some of the test clients. To copy dependencies you can use <a href="http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html">mvn dependency:copy-dependencies</a>.<br />
If you want to run JMeter using maven you probably want to look at <a href="http://mojo.codehaus.org/chronos-maven-plugin/index.html">chronos</a> (although it is not released it seems to work fine):</p>
<ul><a href="http://gabenell.blogspot.com/2009/11/using-maven-chronos-without-external.html">Using Maven Chronos Without an External JMeter Install</a></ul>
<ul><a href="http://blogs.atlassian.com/developer/2009/10/automated_performance_testing_using_jmeter_and_maven.html">Automated performance testing using JMeter and Maven</a></ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/07/07/developing-jmeter-plugins-using-maven/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

