<?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>Saturnboy &#187; testing</title>
	<atom:link href="http://saturnboy.com/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://saturnboy.com</link>
	<description>Code, Work, and Life</description>
	<lastBuildDate>Thu, 01 Mar 2012 22:35:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Intro to OCHamcrest</title>
		<link>http://saturnboy.com/2011/08/intro-to-ochamcrest/</link>
		<comments>http://saturnboy.com/2011/08/intro-to-ochamcrest/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 05:04:09 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1862</guid>
		<description><![CDATA[Sometimes I feel like I write more test code than real code. For unit tests on iOS our stack is OCHamcrest, OCMock, and GHUnit. For functional tests, there&#8217;s nothing better than FoneMonkey. For this post, I&#8217;m going to focus on OCHamcrest. Hamcrest was born in the Java world as the matcher framework in jMock. It [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes I feel like I write more test code than real code. For unit tests on iOS our stack is <a href="https://github.com/jonreid/OCHamcrest">OCHamcrest</a>, <a href="http://www.mulle-kybernetik.com/software/OCMock/">OCMock</a>, and <a href="https://github.com/gabriel/gh-unit">GHUnit</a>. For functional tests, there&#8217;s nothing better than <a href="http://www.gorillalogic.com/fonemonkey">FoneMonkey</a>. For this post, I&#8217;m going to focus on OCHamcrest.</p>
<p><a href="http://code.google.com/p/hamcrest/">Hamcrest</a> was born in the Java world as the matcher framework in <a href="http://www.jmock.org/">jMock</a>. It was quickly extracted into its own framework  and has become somewhat of a monster in the testing world. It&#8217;s now included directly in <a href="http://www.junit.org/">JUnit</a> (since v4.4), and has been ported to many languages (OCHamcrest in Objective-C, <a href="https://github.com/drewbourne/hamcrest-as3">Hamcrest-AS3</a> in Actionscript, <a href="https://github.com/jonreid/PyHamcrest">PyHamcrest</a> in Python, etc.).  Additionally, the matcher concept is generally useful, and Hamcrest is <a href="http://code.google.com/p/hamcrest/wiki/UsesOfHamcrest">used is lots of different places</a> (my favorite is collection filtering with Hamcrest in <a href="http://code.google.com/p/lambdaj/">LambdaJ</a>).</p>
<p>When writing unit tests, OCHamcrest offers lots of advantages over the vanilla SenTest assertions.  First, there&#8217;s a ton of matchers that really make life easy, especially when testing collections like <code>NSArray</code>.  Second, OCHamcrest matchers are very readable in code, almost self-documenting.  Lastly, OCHamcrest automatically provides excellent failure messages when actual is not equal to expected.</p>
<h3>Matching Strings</h3>
<p class="bottom">Some string matching examples:</p>
<ul>
<li><b>is</b> &#8211; match the complete string</li>
<li><b>startsWith</b> &#8211; match the beginning of a string</li>
<li><b>endsWith</b> &#8211; match the end of a string</li>
<li><b>containsString</b> &#8211; match part of the string</li>
<li><b>equalTo</b> &#8211; match the complete string</li>
<li><b>equalToIgnoringCase</b> &#8211; match the complete string but ignore case</li>
<li><b>equalToIgnoringWhiteSpace</b> &#8211; match the complete string but ignore <i>extra</i> whitespace (new line, tab, or double spaces)</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>s <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FooBar&quot;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>s, is<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FooBar&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>s, startsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Foo&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, endsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bar&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, containsString<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;oo&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, equalToIgnoringCase<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;foobar&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; X <span style="color: #2400d9;">\n</span>  Y <span style="color: #2400d9;">\t</span><span style="color: #2400d9;">\t</span>  Z <span style="color: #2400d9;">\n</span>&quot;</span>, equalToIgnoringWhiteSpace<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;X Y Z&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<blockquote class="deeper"><p><b>NOTE:</b> Technically, <code>is</code> isn&#8217;t really a matcher, it&#8217;s a <i>matcher decorator</i> that implicity converts to the <code>equalTo</code> matcher. [thanks Jon!]</p></blockquote>
<h3>Combining Matchers</h3>
<p class="bottom">You can combine multiple matchers with:</p>
<ul>
<li><b>allOf</b> &#8211; AND together all matchers</li>
<li><b>anyOf</b> &#8211; OR togehter all matches</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>s <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FooBar&quot;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>s, allOf<span style="color: #002200;">&#40;</span>startsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Foo&quot;</span><span style="color: #002200;">&#41;</span>, endsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bar&quot;</span><span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, anyOf<span style="color: #002200;">&#40;</span>startsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Foo&quot;</span><span style="color: #002200;">&#41;</span>, startsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bar&quot;</span><span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, anyOf<span style="color: #002200;">&#40;</span>endsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Foo&quot;</span><span style="color: #002200;">&#41;</span>, endsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bar&quot;</span><span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<blockquote class="deeper"><p><b>NOTE:</b> The list of matchers must be <code>nil</code> terminated.</p></blockquote>
<p class="bottom">You can invert a matcher, or multiple matchers, with:</p>
<ul>
<li><b>isNot</b> &#8211; negate the matcher</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>s <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FooBar&quot;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>s, isNot<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;foo&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, isNot<span style="color: #002200;">&#40;</span>endsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Baz&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, isNot<span style="color: #002200;">&#40;</span>allOf<span style="color: #002200;">&#40;</span>startsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Baz&quot;</span><span style="color: #002200;">&#41;</span>, endsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Baz&quot;</span><span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, isNot<span style="color: #002200;">&#40;</span>anyOf<span style="color: #002200;">&#40;</span>startsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Baz&quot;</span><span style="color: #002200;">&#41;</span>, startsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Baz&quot;</span><span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<h3>Matching <code>nil</code></h3>
<p class="bottom">You can match <code>nil</code> with:</p>
<ul>
<li><b>nilValue()</b> &#8211; stands in for <code>nil</code></li>
<li><b>notNilValue()</b> &#8211; stands in for <code>!nil</code></li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSObject</span> <span style="color: #002200;">*</span>o <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
assertThat<span style="color: #002200;">&#40;</span>o, nilValue<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>s <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FooBar&quot;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, notNilValue<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<h3>Matching Classes</h3>
<p class="bottom">You can match an instance&#8217;s class with:</p>
<ul>
<li><b>instanceOf</b> &#8211; match the class</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>s <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FooBar&quot;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, instanceOf<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<h3>Matching Numbers</h3>
<p class="bottom">One of the great pains of Objective-C is typing numbers from primitive types to objects and back again.  OCHamcrest has a variety of matchers the help make life easy.</p>
<ul>
<li><b>assertThatInt</b> &#8211; typed assert that expects an <code>int</code> (other types too: <code>assertThatFloat</code>, <code>assertThatDouble</code>, etc.)</li>
<li><b>equalToInt</b> &#8211; typed equals that takes an <code>int</code> (other types too: <code>equalToFloat</code>, <code>equalToDouble</code>, <code>equalToBool</code>, etc.)</li>
<li><b>closeTo</b> &#8211; match a number with a target number plus or minus a delta (both params are <code>double</code>)</li>
<li><b>lessThan</b> &#8211; match a number less than the given number (param is <code>NSNumber</code>), also <code>lessThanOrEqualTo</code></li>
<li><b>greaterThan</b> &#8211; match a number greater than the given number (param is <code>NSNumber</code>), also <code>greaterThanOrEqualTo</code></li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">assertThatInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">5</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThatFloat<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">3.14</span>, equalToFloat<span style="color: #002200;">&#40;</span>3.14f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThatBool<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">false</span>, equalToBool<span style="color: #002200;">&#40;</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>i <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#93;</span>;
assertThat<span style="color: #002200;">&#40;</span>i, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>i, is<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>f <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>3.14f<span style="color: #002200;">&#93;</span>;
assertThat<span style="color: #002200;">&#40;</span>f, equalToFloat<span style="color: #002200;">&#40;</span>3.14f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>f, is<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>3.14f<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The easiest cleanest approach is to use <code>assertThatInt</code> with <code>equalToInt</code>, the next best option is to use the vanilla <code>assertThat</code> with <code>equalToInt</code>, the most verbose option is to use <code>NSNumber</code> everywhere.</p>
<p class="bottom">It&#8217;s easy to make rough number comparisons too:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>f <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>3.14f<span style="color: #002200;">&#93;</span>;
assertThat<span style="color: #002200;">&#40;</span>f, closeTo<span style="color: #002200;">&#40;</span>3.0f, 0.25f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>f, lessThan<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">4</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>f, greaterThan<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<blockquote class="deeper"><p><b>NOTE:</b> It is a little weird, but <code>closeTo</code> takes <code>double</code> params, but everything else expects <code>NSNumber</code> params.</p></blockquote>
<p class="bottom">Numeric comparisons also work great on dates too:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>now <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//now minus 1000 seconds</span>
<span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>beforeNow <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> dateWithTimeIntervalSinceNow<span style="color: #002200;">:-</span><span style="color: #2400d9;">1000</span><span style="color: #002200;">&#93;</span>; 
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>now, greaterThan<span style="color: #002200;">&#40;</span>beforeNow<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<h3>Matching Arrays</h3>
<p class="bottom">Easily the best part of OCHamcrest is its ability to match lists of objects.  Array matchers are every powerful, but don&#8217;t forget to add the terminating <code>nil</code> to all lists.</p>
<ul>
<li><b>hasItem</b> &#8211; match if given item appears in the list</li>
<li><b>hasItems</b> &#8211; match if all given items appear in the list (in any order)</li>
<li><b>contains</b> &#8211; match exactly the entire array</li>
<li><b>containsInAnyOrder</b> &#8211; match entire array, but in any order</li>
<li><b>hasCountOf</b> &#8211; match the size of the array</li>
<li><b>empty</b> &#8211; match an empty array</li>
</ul>
<p class="bottom">Here some basic array examples:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>a <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> array<span style="color: #002200;">&#93;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>a, is<span style="color: #002200;">&#40;</span>empty<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>a, hasCountOf<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p class="bottom">Here some <code>hasItem</code> examples:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>a <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;a&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;c&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>a, hasItem<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;a&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>a, isNot<span style="color: #002200;">&#40;</span>hasItem<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;X&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>a, hasItem<span style="color: #002200;">&#40;</span>equalToIgnoringCase<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;A&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The last matcher may look a little weird, but remember matchers expect a matcher as their input param, and only default to <code>equalTo</code> if none is given.  Thus, the first matcher <code>hasItem(@"a")</code> can be rewritten as <code>hasItem(equalTo(@"a"))</code>.</p>
<p class="bottom">We repeat the above example, but this time using numbers in our <code>NSArray</code>.  As you can see below, all the number matchers require us to explicitly use <code>equalToInt</code> everywhere:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>a <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span>
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>a, hasItem<span style="color: #002200;">&#40;</span>equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>a, isNot<span style="color: #002200;">&#40;</span>hasItem<span style="color: #002200;">&#40;</span>equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">13</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>a, contains<span style="color: #002200;">&#40;</span>equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#41;</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p class="bottom">Here are some more complex array matchers:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>a <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;a&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;c&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>a, hasItems<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;a&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>a, contains<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;a&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;c&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>a, containsInAnyOrder<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;c&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;a&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>a componentsJoinedByString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;,&quot;</span><span style="color: #002200;">&#93;</span>, is<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;a,b,c&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>And as I show in the last matcher, you can always dump to a string, and just match strings.</p>
<h3>Matching Dictionaries</h3>
<p class="bottom">The dictionary matchers build on the array matchers:</p>
<ul>
<li><b>hasKey</b> &#8211; match a key</li>
<li><b>hasValue</b> &#8211; match a value</li>
<li><b>hasEntry</b> &#8211; match a key-value pair</li>
<li><b>hasEntries</b> &#8211; match a list of k-v pairs</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>d <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>
    <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;valA&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;keyA&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;valB&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;keyB&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;valC&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;keyC&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>d, hasCountOf<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>d, isNot<span style="color: #002200;">&#40;</span>empty<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>d, hasKey<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;keyA&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>d, isNot<span style="color: #002200;">&#40;</span>hasKey<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;keyX&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>d, hasValue<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;valA&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>d, hasEntry<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;keyA&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;valA&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>d, hasEntries<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;keyA&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;valA&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;keyC&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;valC&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<h3>Matcher Error Messages</h3>
<p class="bottom">When a matcher fails, you get a standardize error message of: <code>Expected "foo", but was "bar"</code>.  This default message is easy to modify by using the <code>describedAs()</code> matcher in place of the typical <code>is()</code> matcher.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>s <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;bar&quot;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>s, is<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;foo&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">//Expected &quot;foo&quot;, but was &quot;bar&quot;</span>
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>s, describedAs<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;doh! this should be 'foo'&quot;</span>, equalTo<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;foo&quot;</span><span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">//Expected doh! this should be 'foo', but was &quot;bar&quot;</span>
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>s, describedAs<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;doh! this should be foo, %0, %1&quot;</span>, equalTo<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;foo&quot;</span><span style="color: #002200;">&#41;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;baz&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">42</span><span style="color: #002200;">&#93;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">//Expected doh! this should be foo, &quot;baz&quot;, &lt;42&gt;, but was &quot;bar&quot;</span></pre></div></div>

<blockquote class="deeper"><p><b>NOTE:</b> The argument list for <code>describedAs()</code> <b>MUST</b> end with <code>nil</code> or your tests will crash instantly with no useful error message.</p></blockquote>
<h3>Building a Custom Matcher</h3>
<p class="bottom">Writing your own custom matchers is relatively easy.  Here&#8217;s an example of a matcher that matches the value of some property on an object:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;OCHamcrestIOS/OCHamcrestIOS.h&gt;</span>
<span style="color: #6e371a;">#import &lt;objc/objc-api.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> HasProperty <span style="color: #002200;">:</span> HCBaseMatcher <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>property;
    id&lt;HCMatcher&gt; valueMatcher;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> hasProperty<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aProperty value<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>id&lt;HCMatcher&gt;<span style="color: #002200;">&#41;</span>aValueMatcher;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initWithProperty<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aProperty value<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>id&lt;HCMatcher&gt;<span style="color: #002200;">&#41;</span>aValueMatcher;
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
OBJC_EXPORT id&lt;HCMatcher&gt; hasProperty<span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>property, <span style="color: #a61390;">id</span> valueMatcher<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>We extends <code>HCBaseMatcher</code> with our custom <code>HasProperty</code> class.  We store the name of the property and a value matcher.</p>
<p class="bottom">And the implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;HasProperty.h&quot;</span>
<span style="color: #6e371a;">#import &lt;OCHamcrestIOS/HCDescription.h&gt;</span>
<span style="color: #6e371a;">#import &lt;OCHamcrestIOS/HCWrapInMatcher.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> HasProperty
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> hasProperty<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aProperty value<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>id&lt;HCMatcher&gt;<span style="color: #002200;">&#41;</span>aValueMatcher <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self alloc<span style="color: #002200;">&#93;</span> initWithProperty<span style="color: #002200;">:</span>aProperty value<span style="color: #002200;">:</span>aValueMatcher<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initWithProperty<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aProperty value<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>id&lt;HCMatcher&gt;<span style="color: #002200;">&#41;</span>aValueMatcher <span style="color: #002200;">&#123;</span>
    self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        property <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>aProperty copy<span style="color: #002200;">&#93;</span>;
        valueMatcher <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>aValueMatcher retain<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> dealloc <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>property release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>valueMatcher release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>matches<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>item <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">SEL</span> propertyGetter <span style="color: #002200;">=</span> NSSelectorFromString<span style="color: #002200;">&#40;</span>property<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>item respondsToSelector<span style="color: #002200;">:</span>propertyGetter<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>valueMatcher matches<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>item performSelector<span style="color: #002200;">:</span>propertyGetter<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
            <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> describeTo<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>id&lt;HCDescription&gt;<span style="color: #002200;">&#41;</span>description <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>description appendText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;an object with a property named '%@' with a value of {&quot;</span>, property<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>
        appendDescriptionOf<span style="color: #002200;">:</span>valueMatcher<span style="color: #002200;">&#93;</span>
        appendText<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;}&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span>
&nbsp;
OBJC_EXPORT id&lt;HCMatcher&gt; hasProperty<span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>property, <span style="color: #a61390;">id</span> valueMatcher<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>HasProperty hasProperty<span style="color: #002200;">:</span>property value<span style="color: #002200;">:</span>HCWrapInMatcher<span style="color: #002200;">&#40;</span>valueMatcher<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>When we write a custom matcher, we must implement two methods, <code>matches:</code> to do the matching and <code>describeTo:</code> to provide feedback in case of match failure.  In the above code, we first construct a selector from the given property name, then call the selector to get the actual property value, and finally check if actual matches the expected value (given by the <code>valueMatcher</code>).</p>
<p class="bottom">Usage looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">Person <span style="color: #002200;">*</span>p <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Person personWithFirstName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Joe&quot;</span> andLastname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Doe&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>p, hasProperty<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Joe&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p class="bottom">Or more importantly, we can now use our custom <code>hasProperty</code> matcher to match an arrays of objects:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>a <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span>
    <span style="color: #002200;">&#91;</span>Person personWithFirstName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Joe&quot;</span> andLastname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Doe&quot;</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #002200;">&#91;</span>Person personWithFirstName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Joe&quot;</span> andLastname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Smith&quot;</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #002200;">&#91;</span>Person personWithFirstName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Jane&quot;</span> andLastname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Allen&quot;</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>a, contains<span style="color: #002200;">&#40;</span>
    hasProperty<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Joe&quot;</span><span style="color: #002200;">&#41;</span>,
    hasProperty<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Joe&quot;</span><span style="color: #002200;">&#41;</span>,
    hasProperty<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Jane&quot;</span><span style="color: #002200;">&#41;</span>,
    <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>That&#8217;s it.  Go forth and match.</p>
<p><b>UPDATE:</b> I put the above <code>hasProperty</code> matcher into <a href="https://github.com/jonreid/OCHamcrest/pull/2">a pull request</a>, and Jon Reid accepted it into OCHamcrest v1.6. He even wrote a <a href="http://jonreid.blogs.com/qualitycoding/2011/10/simplify-your-tests-with-ochamcrests-hasproperty.html">nice post</a> about it.  Get the lastest OCHamcrest from <a href="https://github.com/jonreid/OCHamcrest/">github</a>.</p>
<h3>Links</h3>
<ul>
<li><a href="https://github.com/jonreid/OCHamcrest">OCHamcrest</a> &#8211; github repo</li>
<li><a href="http://jonreid.github.com/OCHamcrest/">OCHamcrest Docs</a> &#8211; docs</li>
<li><a href="http://code.google.com/p/hamcrest/">Hamcrest</a> &#8211; on Java</a>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2011/08/intro-to-ochamcrest/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Testing Private Methods in Java</title>
		<link>http://saturnboy.com/2010/11/testing-private-methods-in-java/</link>
		<comments>http://saturnboy.com/2010/11/testing-private-methods-in-java/#comments</comments>
		<pubDate>Fri, 19 Nov 2010 03:09:28 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1648</guid>
		<description><![CDATA[I&#8217;ve been writing a lot of Java lately, and a lot of tests. We always write tests, right? Alas, no cool record-and-playback stuff like FlexMonkey or FoneMonkey, just plain old JUnit 4 tests with plently of Hamcrest goodness. Suddenly, I realized that I really needed to test some private methods. So, a quick google for [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been writing a lot of Java lately, and a lot of tests.  We <i>always</i> write tests, right?  Alas, no cool record-and-playback stuff like <a href="http://www.gorillalogic.com/flexmonkey">FlexMonkey</a> or <a href="http://www.gorillalogic.com/fonemonkey">FoneMonkey</a>, just plain old <a href="http://www.junit.org/">JUnit 4</a> tests with plently of <a href="http://code.google.com/p/hamcrest/">Hamcrest</a> goodness.</p>
<p class="bottom">Suddenly, I realized that I really needed to test some private methods.  So, a quick google for &#8220;testing private methods java&#8221; brings up a <a href="http://www.artima.com/suiterunner/privateP.html">good article</a> by Bill Venners.  He lists all possible options to test private methods:</p>
<ol>
<li>Don&#8217;t test private methods</li>
<li>Give the methods package access</li>
<li>Use a nested test class</li>
<li>Use reflection</li>
</ol>
<p>Basically, the only real one is #4, use reflection.  Bill didn&#8217;t give me the exact code I needed, so lots of googling later I realized that the world is filled with opinionated people (like me), and boy do they love to talk about #1.  I just wanted some code, not a lecture, so I had to write my own code.  Here is that code for anyone else that just wants to test private methods.</p>
<p class="bottom">Imagine you have a class <code>MyClass</code> and it has a private method, <code>myMethod()</code>.  Sorta like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyClass <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> myMethod<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> s<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p class="bottom">Then you could use reflection to invoke the method like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">MyClass myClass <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Method</span> method <span style="color: #339933;">=</span> MyClass.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getDeclaredMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;myMethod&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
method.<span style="color: #006633;">setAccessible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> output <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> method.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>myClass, <span style="color: #0000ff;">&quot;some input&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The real magic is <code>setAccessible(true)</code> which allows the private method to be called outside the class.  And shazam, I can now test all my private methods.  I was really hoping JUnit 4 would provide some additional facilities specifically for testing private methods, but not luck.</p>
<h3>A Better Example</h3>
<p class="bottom">Here&#8217;s a more complete example.  Suppose we have a <code>NovelWriter</code> class that in a feat of API cleanliness only exposes the <code>writeNovel()</code> method.  It happens to have a few private utility methods that we&#8217;d like to test:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NovelWriter <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> writeNovel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//...the magic goes here...</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> shout<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> s.<span style="color: #006633;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>.&quot;</span>, <span style="color: #0000ff;">&quot;!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> countLetters<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> words<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> out <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> word <span style="color: #339933;">:</span> words<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            out.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span> word.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;[^A-Za-z]+&quot;</span>,<span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> out<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I won&#8217;t get into all the details, but it seems easy to imagine a clean API that has private helper methods.  Furthermore, it seems very logical to me to want to bring all methods, both public and private, so I can be sure they are being exercised to the fullest.</p>
<p class="bottom">Our JUnit 4 + Hamcrest test class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NovelWriterTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> NovelWriter novelWriter<span style="color: #339933;">;</span>
&nbsp;
    @BeforeClass
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> beforeClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        novelWriter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NovelWriter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Test
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> privateShout<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NoSuchMethodException</span>,
            <span style="color: #003399;">InvocationTargetException</span>, <span style="color: #003399;">IllegalAccessException</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399;">String</span> input <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;This is magic.&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">Method</span> method <span style="color: #339933;">=</span> NovelWriter.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getDeclaredMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;shout&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        method.<span style="color: #006633;">setAccessible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> output <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> method.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>novelWriter, input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        assertThat<span style="color: #009900;">&#40;</span>output, notNullValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        assertThat<span style="color: #009900;">&#40;</span>output, is<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;THIS IS MAGIC!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Test
    @SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> privateCountLetters<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NoSuchMethodException</span>,
            <span style="color: #003399;">InvocationTargetException</span>, <span style="color: #003399;">IllegalAccessException</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> input <span style="color: #339933;">=</span> <span style="color: #003399;">Arrays</span>.<span style="color: #006633;">asList</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Foo&quot;</span>, <span style="color: #0000ff;">&quot;Foobar123&quot;</span>, <span style="color: #0000ff;">&quot;Foo Bar Baz&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">Method</span> method <span style="color: #339933;">=</span> NovelWriter.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getDeclaredMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;countLetters&quot;</span>, <span style="color: #003399;">List</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        method.<span style="color: #006633;">setAccessible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> output <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> method.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>novelWriter, input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        assertThat<span style="color: #009900;">&#40;</span>output, notNullValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        assertThat<span style="color: #009900;">&#40;</span>output.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, is<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        assertThat<span style="color: #009900;">&#40;</span>output, hasItems<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The nice thing about using the Reflection API like this is that it really doesn&#8217;t get too messy.  I&#8217;m not inspecting anything at runtime, because I know exactly the return type and the types of all the parameters.  I&#8217;m just invoking the method with known inputs, followed by a simple cast on the output type.  And as you can see in the second test above, <code>privateCountLetters()</code>, it&#8217;s not a problem to use generics because we&#8217;re not doing any inspection only invocation.</p>
<p>Happy testing.</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/java/novel.tgz">novel.tgz</a> &ndash; download the complete Eclipse project</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/11/testing-private-methods-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Async Testing with FlexUnit 4</title>
		<link>http://saturnboy.com/2010/02/async-testing-with-flexunit4/</link>
		<comments>http://saturnboy.com/2010/02/async-testing-with-flexunit4/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 20:58:53 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[async]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[flexunit4]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1019</guid>
		<description><![CDATA[I tend to spend a large portion of my development time worrying about the various interfaces across the application. I like to worry about UX (aka the interface between the user and my app). I like to worry about the ORM (aka the interface between the database and my code). And I especially like to [...]]]></description>
			<content:encoded><![CDATA[<p>I tend to spend a large portion of my development time worrying about the various interfaces across the application.  I like to worry about UX (aka the interface between the user and my app).  I like to worry about the ORM (aka the interface between the database and my code).  And I especially like to worry about the client-side service layer (aka the interface between the backend and the frontend).  When I worry, I very quickly find myself writing tests.</p>
<p>The new hotness in Flex testing is, of course, <a href="http://www.gorillalogic.com/flexmonkey">FlexMonkey</a>, developed and open-sourced by my company, <a href="http://www.gorillalogic.com/">Gorilla Logic</a>.  The next best Flex testing platform, and the new hotness in its own right, is <a href="http://www.flexunit.org/">FlexUnit 4</a>, developed and open-sourced by our partners at <a href="http://www.digitalprimates.com/">Digital Primates</a>.  FlexUnit 4 is <i>the</i> Flex 4 unit testing framework.  Because of its awesome async testing support, along with many other great features, it is ideally suited to test client-side service layers.  In this post, I&#8217;m going to explore async testing with FlexUnit 4 to better understand how I can help mitigate the pain of asynchronous backend services that are ever-present in enterprise Flex applications.</p>
<h3>Test: Create Team</h3>
<p>Let&#8217;s imagine I have my favorite data model of teams and players, with a one-to-many relationship between the two entities.  Next, let&#8217;s assume that I wrote a beautiful client-side service layer that has both basic CRUD operations like create team and delete team, and more complex operations like trade player.  I&#8217;d like to cover everything with a set of tests so I can spend my time worrying about other things.</p>
<p class="bottom">Here&#8217;s a simple async test case for the basic create team operation:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span>Test<span style="color: #66cc66;">&#40;</span>async<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createTeam<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> token:AsyncToken = service.<span style="color: #006600;">createTeam</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Los Angeles Lakers'</span><span style="color: #66cc66;">&#41;</span>;
    token.<span style="color: #006600;">addResponder</span><span style="color: #66cc66;">&#40;</span>Async.<span style="color: #006600;">asyncResponder</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #000000; font-weight: bold;">new</span> TestResponder<span style="color: #66cc66;">&#40;</span>createTeam2, fault<span style="color: #66cc66;">&#41;</span>, TIMEOUT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createTeam2<span style="color: #66cc66;">&#40;</span>event:ResultEvent, passThroughData:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> token:AsyncToken = service.<span style="color: #006600;">getAllTeams</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    token.<span style="color: #006600;">addResponder</span><span style="color: #66cc66;">&#40;</span>Async.<span style="color: #006600;">asyncResponder</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #000000; font-weight: bold;">new</span> TestResponder<span style="color: #66cc66;">&#40;</span>createTeam3, fault<span style="color: #66cc66;">&#41;</span>, TIMEOUT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createTeam3<span style="color: #66cc66;">&#40;</span>event:ResultEvent, passThroughData:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> teams:ArrayCollection = event.<span style="color: #006600;">result</span> as ArrayCollection;
    assertThat<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Team not created'</span>, <span style="color: #ff0000;">'Los Angeles Lakers'</span>, inArray<span style="color: #66cc66;">&#40;</span>teams.<span style="color: #006600;">toArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>First, we create a new team, then we load all the teams, and lastly, we verify that the newly created team is in the list.  There are two important things to note: async stuff is everywhere (<code>[Test(async)]</code> metadata, <code>AsyncToken</code>, <code>AsyncResponder</code>, etc.), and there is a chain of functions (<code>createTeam()</code> chains to <code>createTeam2()</code> which chains to <code>createTeam3()</code>).  In particular, the <b>chain</b> pattern is characteristic of any async testing.  Every single non-trivial async test involves a chain of function calls to do the work of testing an asynchronous backend.</p>
<p class="bottom">Here&#8217;s a simple diagram of the chain for the create team test:</p>
<div class="span-14 last" style="min-height:134px;">
<img src="http://saturnboy.com/wp-content/uploads/2010/02/diagram_test-create-team.png" alt="create-team" width="528" height="124" /></div>
<p>Each diagram box is just a logic operation in our test, and they also happen to correspond exactly to the functions that make up the test chain.</p>
<h3>Test: Trade Player</h3>
<p>When testing the more complex client-side service layer operations, or simply writing more complex tests, the chain pattern often develops branches and sub-chains as various pieces of state are verified asynchronously.</p>
<p class="bottom">A good example is the trade player operation, which we might test using a chain with two branches: one to verify the player was removed from old team, and one to verify the player was added to the new team.  Here&#8217;s the diagram:</p>
<div class="span-14 last" style="min-height:292px;">
<img src="http://saturnboy.com/wp-content/uploads/2010/02/diagram_test-trade-player.png" alt="trade-player" width="528" height="282" /></div>
<p>We don&#8217;t really care what goes on inside the client-side service layer to achieve this, or even what happens on the backend (it&#8217;s probably just as simple as changing the <code>team_id</code> column on the <code>players</code> table to the new team&#8217;s id).  We only care that the test passes.</p>
<p class="bottom">And the accompanying test code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span>Test<span style="color: #66cc66;">&#40;</span>async<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> tradePlayer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> token:AsyncToken = service.<span style="color: #006600;">tradePlayer</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Carmelo Anthony'</span>, <span style="color: #ff0000;">'Denver Nuggets'</span>, <span style="color: #ff0000;">'Cleveland Cavaliers'</span><span style="color: #66cc66;">&#41;</span>;
    token.<span style="color: #006600;">addResponder</span><span style="color: #66cc66;">&#40;</span>Async.<span style="color: #006600;">asyncResponder</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #000000; font-weight: bold;">new</span> TestResponder<span style="color: #66cc66;">&#40;</span>tradePlayer2, fault<span style="color: #66cc66;">&#41;</span>, TIMEOUT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> tradePlayer2<span style="color: #66cc66;">&#40;</span>event:ResultEvent, passThroughData:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> token:AsyncToken = service.<span style="color: #006600;">getPlayersByTeam</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Denver Nuggets'</span><span style="color: #66cc66;">&#41;</span>;
    token.<span style="color: #006600;">addResponder</span><span style="color: #66cc66;">&#40;</span>Async.<span style="color: #006600;">asyncResponder</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #000000; font-weight: bold;">new</span> TestResponder<span style="color: #66cc66;">&#40;</span>tradePlayer3, fault<span style="color: #66cc66;">&#41;</span>, TIMEOUT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">var</span> token2:AsyncToken = service.<span style="color: #006600;">getPlayersByTeam</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Cleveland Cavaliers'</span><span style="color: #66cc66;">&#41;</span>;
    token2.<span style="color: #006600;">addResponder</span><span style="color: #66cc66;">&#40;</span>Async.<span style="color: #006600;">asyncResponder</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #000000; font-weight: bold;">new</span> TestResponder<span style="color: #66cc66;">&#40;</span>tradePlayer4, fault<span style="color: #66cc66;">&#41;</span>, TIMEOUT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> tradePlayer3<span style="color: #66cc66;">&#40;</span>event:ResultEvent, passThroughData:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> players:ArrayCollection = event.<span style="color: #006600;">result</span> as ArrayCollection;
    assertThat<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Traded player not removed from old team'</span>, <span style="color: #ff0000;">'Carmelo Anthony'</span>, <span style="color: #0066CC;">not</span><span style="color: #66cc66;">&#40;</span>inArray<span style="color: #66cc66;">&#40;</span>players.<span style="color: #006600;">toArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> tradePlayer4<span style="color: #66cc66;">&#40;</span>event:ResultEvent, passThroughData:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> players:ArrayCollection = event.<span style="color: #006600;">result</span> as ArrayCollection;
    assertThat<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Traded player not added to new team'</span>, <span style="color: #ff0000;">'Carmelo Anthony'</span>, inArray<span style="color: #66cc66;">&#40;</span>players.<span style="color: #006600;">toArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The interesting part occurs in the second step in the chain, <code>tradePlayer2()</code>.  In this function, we use a pair of <code>AsyncToken</code>s, to fork the chain into two sub-chains.  One sub-chain gets all the players on the old team and verifies that the traded player has been removed.  And the other sub-chain gets all the players on the new team and verifies that the trade player has been added.</p>
<h3>A Better Approach</h3>
<p>Right now, the chained function approach is the only approach for testing an asynchronous client-side service layer.  As another example, you can see the chained function approach appears again when I tested an <a href="http://www.adobe.com/products/livecycle/dataservices/">LCDS</a>-powered backend in my <a href="http://www.insideria.com/2009/12/getting-real-with-lcds-3-beta.html">Getting Real with LCDS, Part 1</a> article at <a href="http://www.insideria.com/">InsideRIA.com</a>.</p>
<p>There has got to be something better, right?  Chained functions work fine, but boy are they ugly looking in code.  I&#8217;ve been having a <a href="http://forums.adobe.com/thread/568166">discussion</a> on the FlexUnit forums about better async testing.  The general wisdom is that one could use the <code>Sequence</code> interfaces to build an async action and have the <a href="http://docs.flexunit.org/asdocs/org/fluint/sequence/SequenceRunner.html"><code>SequenceRunner</code></a> manage the chain.  Currently, the best documentation on Sequences is the old <a href="http://code.google.com/p/fluint/wiki/Sequences">Fluint wiki doc</a>.  In enterprise Flex development, async backends tend to swarm like locusts, so I hope to have some code to show soon to streamline the testing process.</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/flexunit4/test_basketball/TestBasketball.html">TestBasketball</a> (<a href="http://saturnboy.com/proj/flexunit4/test_basketball/TestRunner.html">run tests</a>, <a href="http://saturnboy.com/proj/flexunit4/test_basketball/srcview/index.html">view source</a>, <a href="http://saturnboy.com/proj/flexunit4/test_basketball/srcview/TestBaketball.zip">download</a>)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/02/async-testing-with-flexunit4/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Testing async services with Fluint</title>
		<link>http://saturnboy.com/2009/03/fluint-async-testing/</link>
		<comments>http://saturnboy.com/2009/03/fluint-async-testing/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 04:58:45 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[async]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[flexmonkey]]></category>
		<category><![CDATA[fluint]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=307</guid>
		<description><![CDATA[We&#8217;re pretty big on testing at Gorilla Logic, and in the world of Flex that usually means using FlexMonkey to test the UI and using FlexUnit to test the code. Alas, it is a huge pain in the ass to correctly test the many async objects and services inherent in any Flex app with FlexUnit. [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re pretty big on testing at <a href="http://www.gorillalogic.com/">Gorilla Logic</a>, and in the world of Flex that usually means using <a href="http://code.google.com/p/flexmonkey/">FlexMonkey</a> to test the UI and using <a href="http://opensource.adobe.com/wiki/display/flexunit/Flexunit">FlexUnit</a> to test the code.  Alas, it is a huge pain in the ass to correctly test the many async objects and services inherent in any Flex app with FlexUnit.  Enter <a href="http://code.google.com/p/fluint/">Fluint</a>, an superior Flex unit tesing framework by the cool guys at <a href="http://www.digitalprimates.net/">digital primates</a> (no relation).  Fluint is the heir apparent to take over the unit testing crown from the venerable FlexUnit.  So let&#8217;s take Fluint and its enhanced async testing support for a spin.</p>
<h5>Service Layer</h5>
<p class="bottom">First, assume we have a nice service layer in Flex that talks asynchronously to our backend.  Just something simple to start:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> MyService <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> getSomething<span style="color: #000000;">&#40;</span>result<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Function</span><span style="color: #000066; font-weight: bold;">,</span> fault<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Function</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>AsyncToken <span style="color: #000000;">&#123;</span>
        <span style="color: #009900; font-style: italic;">//call the backend</span>
        <span style="color: #6699cc; font-weight: bold;">var</span> token<span style="color: #000066; font-weight: bold;">:</span>AsyncToken = backend<span style="color: #000066; font-weight: bold;">.</span>getSomething<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
        <span style="color: #009900; font-style: italic;">//wire the callbacks to the result</span>
        token<span style="color: #000066; font-weight: bold;">.</span>addResponder<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> AsyncResponder<span style="color: #000000;">&#40;</span>result<span style="color: #000066; font-weight: bold;">,</span> fault<span style="color: #000066; font-weight: bold;">,</span> token<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">return</span> token<span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>In this example, our service only has one method, <code>getSomething()</code> that takes two callback functions.  It simply calls the backend method, wires up the callbacks (which get called when the backend method returns a result), and returns the token.  It is <b>absolutely critical</b> that our callback-powered service method return the <code>AsyncToken</code>.  The reason for this will become apparent.</p>
<p class="bottom">We might use our service like this:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Application</span></span>
<span style="color: #000000;">        xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span></span>
<span style="color: #000000;">        creationComplete=<span style="color: #ff0000;">&quot;complete()&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #339933;">&lt;mx:Script&gt;</span>
<span style="color: #339933;">    &lt;![CDATA[</span>
<span style="color: #339933;">        import com.saturnboy.services.MyService;</span>
<span style="color: #339933;">        private var service:MyService;</span>
&nbsp;
<span style="color: #339933;">        private function complete():void {</span>
<span style="color: #339933;">            service = new MyService();</span>
<span style="color: #339933;">            service.getSomething(resultHandler, faultHandler);</span>
<span style="color: #339933;">        }</span>
&nbsp;
<span style="color: #339933;">        private function resultHandler(result:Object, token:Object=null):void {</span>
<span style="color: #339933;">            lbl.text = result.result.name;</span>
<span style="color: #339933;">        }</span>
&nbsp;
<span style="color: #339933;">        public function faultHandler(error:Object, token:Object=null):void {</span>
<span style="color: #339933;">            lbl.text = 'fault';</span>
<span style="color: #339933;">        }</span>
<span style="color: #339933;">    ]]&gt;</span>
<span style="color: #339933;">    &lt;/mx:Script&gt;</span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Label</span> id=<span style="color: #ff0000;">&quot;lbl&quot;</span> text=<span style="color: #ff0000;">&quot;initial&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>We make a call our service, and then use the callbacks to alter the UI however we want depending on the result.  In common usage, the fact that our service returns an <code>AsyncToken</code> is worthless, it might as well return <code>void</code>.  So, why did I say this is critical?  Throw Fluint testing into the mix and it&#8217;s &#8220;Show em what&#8217;s behind door number 2, Johnny!&#8221;</p>
<h5>Fluint Testing</h5>
<p class="bottom">Fluint provides two different async wrapper methods: <code>asyncHandler</code> and <code>asyncResponder</code>.  The first allows a test to be wired to an async method by events, the second allows a test to be wired to an async method by a responder.  Since the service method we&#8217;re trying to test doesn&#8217;t throw any events, we&#8217;ll need to use the latter.  So inside a Fluint test case, we have our test method:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testGetSomething<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #009900; font-style: italic;">//call service with dummy callback</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> token<span style="color: #000066; font-weight: bold;">:</span>AsyncToken = service<span style="color: #000066; font-weight: bold;">.</span>getSomething<span style="color: #000000;">&#40;</span>dummyResult<span style="color: #000066; font-weight: bold;">,</span> dummyFault<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
    <span style="color: #009900; font-style: italic;">//create async test responder</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> responder<span style="color: #000066; font-weight: bold;">:</span>IResponder = asyncResponder<span style="color: #000000;">&#40;</span>
            <span style="color: #0033ff; font-weight: bold;">new</span> TestResponder<span style="color: #000000;">&#40;</span>testHandler<span style="color: #000066; font-weight: bold;">,</span> faultHandler<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">1000</span><span style="color: #000066; font-weight: bold;">,</span> token<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
    <span style="color: #009900; font-style: italic;">//wire test responder as 2nd callback</span>
    token<span style="color: #000066; font-weight: bold;">.</span>addResponder<span style="color: #000000;">&#40;</span>responder<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> testHandler<span style="color: #000000;">&#40;</span>result<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000066; font-weight: bold;">,</span> passThroughData<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
    assertEquals<span style="color: #000000;">&#40;</span><span style="color: #990000;">'something'</span><span style="color: #000066; font-weight: bold;">,</span> result<span style="color: #000066; font-weight: bold;">.</span>result<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">name</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The trick is to wire a second callback via Fluint&#8217;s <code>asyncResponder</code> helper that actually does the testing, and just give the original service call some dummy callbacks.  Note that if the service method didn&#8217;t return its <code>AsyncToken</code> there would be no way to wire a second callback.  The Fluint async helper do two import operations: they handle the event or call the callback AND they mark the test method as an async method so the result is correctly reported by the test harness.  You can read more about <a href="http://code.google.com/p/fluint/wiki/AsyncTest">Async Testing</a> in Fluint&#8217;s wiki.  The rest of Fluint is your standard chain of crap borrowed from JUnit: test runner, test suites, and test cases.</p>
<blockquote class="deeper"><p><b>Digging Deeper:</b> It is equally critical to use dummy callbacks in the original service method call because in a failure situation they will cause Flash Player to error out instead of being caught by Fluint and reported as a test failure.</p></blockquote>
<h5>Files</h5>
<p>The complete code is up on <a href="http://www.github.com/">GitHub</a> here: <a href="http://github.com/saturnboy/test_fluint_async/tree/master">test_fluint_async</a>.  The code is MIT licensed and includes a working fluint.swc (see below) plus a mock async backend (so timeouts and faults are easily testable).</p>
<p>Alas, Fluint v1.1.0 was built incorrectly and is missing the <code>TestResponder</code> class (see <a href="http://code.google.com/p/fluint/issues/detail?id=35&#038;can=1">issue 35</a>).  So if you want to try out Fluint in your project, I recommend you grab it from svn and build the swc yourself.  Hopefully, this will all be fixed in the next release.</p>
<p><strong>UPDATE:</strong> Fluint v1.1.1 was release on May 1, 2009 and fixes this issues and a few others.  Download it <a href="http://code.google.com/p/fluint/downloads/list">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2009/03/fluint-async-testing/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

