Flex project setup - Part IV. Mock Data as an alternative Data Provider

This article is part of group of posts on Flex project setup or five use cases for multiple source paths linking.


If you have been careful to properly isolate the code that has to do with accessing external data, then you can swap actual service calls with mock data mode as easily as you would swap between equivalent providers.

It can prove useful in these different contexts:

  • Whenever you want to test some specific use case or use scenarios. Check how your application reacts when malformed or incomplete data are returned by the web-service.
  • When it is still the early stages of application development and services are only available when connected to the company network. If you face the prospect to burn the midnight oil to meet a deadline, mock data give you the opportunity to do it in the comfort of your home.
  • When it is the very early stages of development. The back-end is still being modified. Services can be unavailable sometimes for hours in a row. With mock data, you switch to them and continue development. Less tension in the team!
  • Worse! The webservices are incomplete or sometimes return invalid data. You are asked to provide clear information on what the input or output of the webservices should look like. The mock-data mode let you experiment with different data format, until you are confident as to your requirements. You can then send clear instructions to the back-end team. Even better, as your code has been made to work exactly as if the data were returned by the actual webservices, you were able to write unit-tests. If the back-end guy sends you data that break any of the test, the pressure is on them to get their code to work as your application expect.

As can be seen in the screenshot of the Package Explorer within Flash Builder, Test-mock includes a reference to the App/src, service-xmlWidged folder introduced in Part III and a new folder mock-xmlWidged. App/src is required for access to classes that capture Virtual Objects (VOs) used internally. It is also required to run the full application in mock data mode. A reference to service-xmlWidged is required for access to the Provider interface (IArtProvider.as) as well as access to the utility classes that convert xml data to internal VOs (ArtXmlConverter.as).

Test-mock overwrites ArtProvider.as and provide a new class to store the data that capture the input and output to the services.

provider.artOfWar.ArtProvider.as


package provider.artOfWar
{
import com.widged.warStories.pages.artOfWar.events.ArtContentEvent;
import com.widged.warStories.pages.artOfWar.vo.ArtContentVO;

import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;

import mx.rpc.events.ResultEvent;

public class ArtProvider extends EventDispatcher implements IArtProvider
{
private var _xmlConverter:ArtXmlConverter = new ArtXmlConverter();

public function ArtProvider(baseUrl:String)
{
}

public function getArtContent(localeCode:String):void
{
trace("[Mock][ArtProvider.getArtContent]", localeCode)
var xml:XML = ArtData['content_' + localeCode];
var vo:ArtContentVO = _xmlConverter.artContentFromXml(xml);
dispatchEvent(new ArtContentEvent(ArtContentEvent.RESULT, vo));
}
}
}

package provider.artOfWar.ArtData


package provider.artOfWar
{
public class ArtData
{

public static var content_en_US:XML = XML(<content>
<h1><![CDATA[Mock Data]]></h1>
<h2><![CDATA[Mock Data]]></h2>
<body>
<![CDATA[
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sed nibh libero. Donec vitae sem nec erat adipiscing aliquet eu at augue. Phasellus nibh augue, consectetur eu vehicula vel, suscipit at massa. Pellentesque justo augue, sodales at varius ut, ultricies ac ligula. Vivamus a malesuada massa. Sed id odio quis mi adipiscing condimentum id at quam. Maecenas eleifend luctus diam, in pulvinar neque bibendum non. Fusce iaculis felis id purus iaculis suscipit. Donec porttitor arcu non velit pulvinar et imperdiet justo faucibus. Nunc et lacus mi, nec porta arcu. Sed pretium nulla vel odio congue eu convallis eros viverra. Vivamus molestie elit sit amet purus luctus posuere. Cras nec arcu at nunc ornare rhoncus. Pellentesque massa eros, vestibulum quis adipiscing in, tincidunt id erat.</p>
<p>Sed vitae arcu vel ante porta tincidunt a vel ipsum. Pellentesque tempor, urna vel rhoncus dignissim, urna erat placerat mi, eget sagittis diam enim nec nunc. Quisque euismod auctor nulla vitae dignissim. Phasellus in dictum enim. Nam vel metus lorem, vel accumsan quam. Donec ac volutpat dolor. Sed tincidunt tellus vitae quam mollis eleifend eu ut magna. Nullam est nisl, cursus sed feugiat eget, consectetur ac purus. Nulla quis sem quam. Nam eleifend augue sed elit eleifend adipiscing blandit nulla condimentum. Nam condimentum tempor neque a sodales. Nunc hendrerit mi quis massa congue gravida. Pellentesque sit amet molestie turpis. Curabitur vel felis eu turpis pulvinar suscipit.</p>
<p>Maecenas a quam vel lacus suscipit vulputate. Aliquam sodales, elit eget laoreet luctus, tortor nunc congue sem, eget ullamcorper lacus urna vitae augue. Nullam tellus neque, suscipit et fringilla sed, aliquet ut massa. Sed sollicitudin consequat suscipit. Fusce sit amet elit quam. Fusce pretium malesuada enim, ut malesuada lectus tristique at. Aenean arcu tortor, scelerisque ac ultrices sit amet, congue in ligula. Donec consectetur, leo eu congue tempus, tellus felis molestie erat, eu interdum risus felis bibendum purus. Fusce enim odio, gravida eget consequat quis, luctus eget mi. Pellentesque quis odio in diam sollicitudin pellentesque. Fusce dignissim sem non justo blandit placerat ultrices augue eleifend. Cras ipsum metus, suscipit sed placerat quis, tempus viverra leo.</p>
]]>
</body>
</content>);

public static var content_fr_FR:XML = XML(<content>
<h1><![CDATA[Données Bidon]]></h1>
<h2><![CDATA[Données Bidon]]></h2>
<body>
<![CDATA[
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sed nibh libero. Donec vitae sem nec erat adipiscing aliquet eu at augue. Phasellus nibh augue, consectetur eu vehicula vel, suscipit at massa. Pellentesque justo augue, sodales at varius ut, ultricies ac ligula. Vivamus a malesuada massa. Sed id odio quis mi adipiscing condimentum id at quam. Maecenas eleifend luctus diam, in pulvinar neque bibendum non. Fusce iaculis felis id purus iaculis suscipit. Donec porttitor arcu non velit pulvinar et imperdiet justo faucibus. Nunc et lacus mi, nec porta arcu. Sed pretium nulla vel odio congue eu convallis eros viverra. Vivamus molestie elit sit amet purus luctus posuere. Cras nec arcu at nunc ornare rhoncus. Pellentesque massa eros, vestibulum quis adipiscing in, tincidunt id erat.</p>
<p>Sed vitae arcu vel ante porta tincidunt a vel ipsum. Pellentesque tempor, urna vel rhoncus dignissim, urna erat placerat mi, eget sagittis diam enim nec nunc. Quisque euismod auctor nulla vitae dignissim. Phasellus in dictum enim. Nam vel metus lorem, vel accumsan quam. Donec ac volutpat dolor. Sed tincidunt tellus vitae quam mollis eleifend eu ut magna. Nullam est nisl, cursus sed feugiat eget, consectetur ac purus. Nulla quis sem quam. Nam eleifend augue sed elit eleifend adipiscing blandit nulla condimentum. Nam condimentum tempor neque a sodales. Nunc hendrerit mi quis massa congue gravida. Pellentesque sit amet molestie turpis. Curabitur vel felis eu turpis pulvinar suscipit.</p>
<p>Maecenas a quam vel lacus suscipit vulputate. Aliquam sodales, elit eget laoreet luctus, tortor nunc congue sem, eget ullamcorper lacus urna vitae augue. Nullam tellus neque, suscipit et fringilla sed, aliquet ut massa. Sed sollicitudin consequat suscipit. Fusce sit amet elit quam. Fusce pretium malesuada enim, ut malesuada lectus tristique at. Aenean arcu tortor, scelerisque ac ultrices sit amet, congue in ligula. Donec consectetur, leo eu congue tempus, tellus felis molestie erat, eu interdum risus felis bibendum purus. Fusce enim odio, gravida eget consequat quis, luctus eget mi. Pellentesque quis odio in diam sollicitudin pellentesque. Fusce dignissim sem non justo blandit placerat ultrices augue eleifend. Cras ipsum metus, suscipit sed placerat quis, tempus viverra leo.</p>
]]>
</body>
</content>);

}
}

And the result is illustrated in this screenshot:

Note that as the mock project ends up running the exact same application than the main production code, I take the precaution to add a colourful border around the application window. A useful reminder that this is not the actual application and may sometimes act slightly differently.

Because a main application is required within the Test-Mock project (running the Main app from the linked App/src folder doesn't work), it is good practice to use a minimalistic AppLauncher to minimize the risk that some code gets updated in one location but not the other. This trick is explained in Part V.

Downloads

(Require Flash Builder 4)

Powered by Drupal, an open source content management system