<?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>Ayone Blog &#187; Testing</title>
	<atom:link href="http://ayonesoftware.com/blog/category/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://ayonesoftware.com/blog</link>
	<description>Sharing ideas on Rich Internet Application Development</description>
	<lastBuildDate>Tue, 18 Aug 2009 06:40:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Asynchronous Testing using FlexUnit 4</title>
		<link>http://ayonesoftware.com/blog/2009/08/asynchronous-testing-using-flexunit-4/</link>
		<comments>http://ayonesoftware.com/blog/2009/08/asynchronous-testing-using-flexunit-4/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 15:30:50 +0000</pubDate>
		<dc:creator>Adrian Aioanei</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://ayonesoftware.com/blog/?p=95</guid>
		<description><![CDATA[As you might have known there is a new version of FlexUnit in development on the opensource site at Adobe http://opensource.adobe.com/wiki/display/flexunit/FlexUnit for a few months now. This is a new breed between FlexUnit 1 and Fluint. Before delving into the asynchronous topic i would like to mention some improvements to the framework:
1. You are not [...]]]></description>
			<content:encoded><![CDATA[<p>As you might have known there is a new version of FlexUnit in development on the opensource site at Adobe <a href="http://opensource.adobe.com/wiki/display/flexunit/FlexUnit">http://opensource.adobe.com/wiki/display/flexunit/FlexUnit</a> for a few months now. This is a new breed between FlexUnit 1 and <a href="http://code.google.com/p/fluint/">Fluint</a>. Before delving into the asynchronous topic i would like to mention some improvements to the framework:</p>
<p>1. You are not any more required to explicitly extend TestSuite and TestCase classes. For the suite you just mark it as being a suite by placing [Suite] metadata tag. All test case classes that you want the suite to run should then be added as public properties in this class. The TestCase class does not require having methods that begin with &#8220;test&#8221;. Instead you mark the test methods by [Test] metadata.</p>
<p>2. The runner is externalized, requiring FlexUnit4UIRunner.swc for default runner. You specify the runner which a suite used by placing [RunWith] metadata tag, default runner looking like [RunWith("org.flexunit.runners.Suite")]. This being the case you can always create a custom runner and add new capabilities/functionalities in test runs.</p>
<p>3. Flash Builder 4 has a built in mechanism for testing, allowing one to choose what suites or test cases to run and display results in FlexUnit Results view. I wouldn&#8217;t recommend this approach right now as it has some huge memory issues which eventually will get the builder to a halt.</p>
<p>For a big picture about new capabilities of FlexUnit 4 go to <a href="http://opensource.adobe.com/wiki/display/flexunit/FlexUnit+4+feature+overview">http://opensource.adobe.com/wiki/display/flexunit/FlexUnit+4+feature+overview</a></p>
<p>Now on the asynchronous area, which has been a lot improved since the addition of Fluint. To mark a method as being part of an async process, you just add &#8220;async&#8221; under [Test] metadata [Test(async)]. This will let you access the asynchronous capabilities of the framework. The one that we&#8217;ll exemplify is usage of a RemoteObject call.</p>
<p>First we build the basic test class:</p>
<div class="codecolorer-container actionscript " style="overflow:auto;white-space:nowrap;width:660px;height:300px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br /></div></td><td><div class="actionscript codecolorer" style="font-family:Monaco,Lucida Console,monospace">package<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <br />
<span class="kw3">import</span> mx.<span class="me1">rpc</span>.<span class="me1">remoting</span>.<span class="me1">RemoteObject</span>;<br />
<br />
<span class="kw3">public</span> <span class="kw2">class</span> TestRemoteObject<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">//------------------------------------------------------------</span><br />
&nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; <span class="co1">// Properties</span><br />
&nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; <span class="co1">//------------------------------------------------------------</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp;* Service to be used in testing async calls.<br />
&nbsp; &nbsp; &nbsp;* @private <br />
&nbsp; &nbsp; &nbsp;*/</span> <br />
&nbsp; &nbsp; <span class="kw3">private</span> <span class="kw3">static</span> <span class="kw2">var</span> service:RemoteObject;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">//------------------------------------------------------------</span><br />
&nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; <span class="co1">// Methods</span><br />
&nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; <span class="co1">//------------------------------------------------------------</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="br0">&#91;</span>BeforeClass<span class="br0">&#93;</span><br />
&nbsp; &nbsp; <span class="kw3">public</span> <span class="kw3">static</span> <span class="kw2">function</span> setUp<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; service = <span class="kw2">new</span> RemoteObject<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; service.<span class="me1">endpoint</span> = <span class="st0">&quot;yourEndpointHere&quot;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>A simple declaration of the RemoteObject and a [BeforeClass] marked method. What [BeforeClass] states is that the method is called only once before the whole class is executed in the test queue. We just setup the RemoteObject with any valid endpoint. The next and final step of the test case is actually testing a method of the service:</p>
<div class="codecolorer-container actionscript " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="actionscript codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="br0">&#91;</span>Test<span class="br0">&#40;</span>async<span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<span class="kw3">public</span> <span class="kw2">function</span> testMyMethod<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">var</span> token:AsyncToken = service.<span class="me1">myMethod</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; token.<span class="me1">addResponder</span><span class="br0">&#40;</span> Async.<span class="me1">asyncResponder</span><span class="br0">&#40;</span> <span class="kw3">this</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> TestResponder<span class="br0">&#40;</span> verifyResult, verifyFault <span class="br0">&#41;</span>, <span class="nu0">2000</span> <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
protected <span class="kw2">function</span> verifyResult<span class="br0">&#40;</span> <span class="kw3">e</span>:ResultEvent, token:AsyncToken <span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Assert.<span class="me1">assertTrue</span><span class="br0">&#40;</span> <span class="st0">&quot;Result is what i want it to be&quot;</span>, <span class="kw3">e</span>.<span class="me1">result</span> is ArrayCollection <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
protected <span class="kw2">function</span> verifyFault<span class="br0">&#40;</span> <span class="kw3">e</span>:FaultEvent, token:AsyncToken <span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Assert.<span class="me1">fail</span><span class="br0">&#40;</span> <span class="st0">&quot;Unintended fault from service&quot;</span> <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>Taking it from the in out, we define a TestResponder which is nothing else than a simple Responder, only that it adds an extra parameter &#8220;passThroughData&#8221; (more on this in a later post) to the result and fault methods. This responder is then added into the async capabilities of the framework using Async.asyncResponder, registering the test case (this &#8211; first param), responder (which we just built) and the timeout for call failure. On the verifyResult method you can assert everything you want from the result object and under verifyFault, if this fault is not intentional, you can just Assert.fail, telling the framework that the call failed.</p>
<p>Another goodie about the asynchronous testing in FlexUnit 4 is that you&#8217;re no longer needed to chain multiple async methods with events and listeners. You can accomplish this by using &#8220;order&#8221; param under [Test] metadata. So for the first async method tested you would have [Test(async,order=1)], for the second one [Test(async,order=2)] and so on. The framework will route them handy async one after another.</p>
<p>Next step would be to build the suite class, which as described above should look very simple:</p>
<div class="codecolorer-container actionscript " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="actionscript codecolorer" style="font-family:Monaco,Lucida Console,monospace">package<br />
<span class="br0">&#123;</span><br />
<br />
<span class="br0">&#91;</span>Suite<span class="br0">&#93;</span><br />
<span class="br0">&#91;</span>RunWith<span class="br0">&#40;</span><span class="st0">&quot;org.flexunit.runners.Suite&quot;</span><span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<span class="kw3">public</span> <span class="kw2">class</span> TestSuite<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">var</span> testRO:TestRemoteObject;<br />
<span class="br0">&#125;</span><br />
<br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>And the final step is getting everything to work under an Application. The one that does that is FlexUnitCore, which as the name states is the core component coupling the suite and test cases with the runner:</p>
<div class="codecolorer-container mxml " style="overflow:auto;white-space:nowrap;width:660px;height:300px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br /></div></td><td><div class="mxml codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="sc3"><span class="re1">&lt;s:Application</span> xmlns:fx=<span class="st0">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span><br />
<span class="sc3">&nbsp; &nbsp; xmlns:s=<span class="st0">&quot;library://ns.adobe.com/flex/spark&quot;</span></span><br />
<span class="sc3">&nbsp; &nbsp; xmlns:flexUnitUIRunner=<span class="st0">&quot;http://www.adobe.com/2009/flexUnitUIRunner&quot;</span></span><br />
<span class="sc3">&nbsp; &nbsp; creationComplete=<span class="st0">&quot;creationCompleteHandler( event )&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;fx:Script</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3">&lt;!<span class="br0">&#91;</span>CDATA<span class="br0">&#91;</span></span><br />
<span class="sc3">&nbsp; &nbsp; </span><br />
<span class="sc3">&nbsp; &nbsp; import mx.events.FlexEvent;</span><br />
<span class="sc3">&nbsp; &nbsp; </span><br />
<span class="sc3">&nbsp; &nbsp; import org.flexunit.runner.FlexUnitCore;</span><br />
<br />
<span class="sc3">&nbsp; &nbsp; protected function creationCompleteHandler<span class="br0">&#40;</span> event:FlexEvent <span class="br0">&#41;</span>:void</span><br />
<span class="sc3">&nbsp; &nbsp; <span class="br0">&#123;</span></span><br />
<span class="sc3">&nbsp; &nbsp; &nbsp; &nbsp; var core:FlexUnitCore = new FlexUnitCore<span class="br0">&#40;</span><span class="br0">&#41;</span>;</span><br />
<span class="sc3">&nbsp; &nbsp; &nbsp; &nbsp; core.addListener<span class="br0">&#40;</span> runner <span class="br0">&#41;</span>;</span><br />
<span class="sc3">&nbsp; &nbsp; &nbsp; &nbsp; </span><br />
<span class="sc3">&nbsp; &nbsp; &nbsp; &nbsp; core.run<span class="br0">&#40;</span> TestSuite <span class="br0">&#41;</span>;</span><br />
<span class="sc3">&nbsp; &nbsp; <span class="br0">&#125;</span></span><br />
<span class="sc3">&nbsp; &nbsp; </span><br />
<span class="sc3">&nbsp; &nbsp; <span class="br0">&#93;</span><span class="br0">&#93;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/fx:Script</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;flexUnitUIRunner:TestRunnerBase</span> id=<span class="st0">&quot;runner&quot;</span> width=<span class="st0">&quot;100%&quot;</span> height=<span class="st0">&quot;100%&quot;</span><span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; <br />
<span class="sc3"><span class="re1">&lt;/s:Application</span><span class="re2">&gt;</span></span></div></td></tr></tbody></table></div>
<p>The runner, being externalized, is added into the works using core.addListener, thus receiving events from core regarding the tests. Simply call core.run() on the suite and hope that green checks are everywhere. To view the detailed code for async capabilities in FlexUnit go to <a href="http://opensource.adobe.com/svn/opensource/flexunit/branches/4.x/FlexUnit4/src/org/flexunit/async/">http://opensource.adobe.com/svn/opensource/flexunit/branches/4.x/FlexUnit4/src/org/flexunit/async/</a>.</p>
<p>Download example source code from <a href="http://ayonesoftware.com/blog/wp-content/uploads/2009/08/AsyncTesting.zip">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://ayonesoftware.com/blog/2009/08/asynchronous-testing-using-flexunit-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
