Home > Uncategorized > Unit test to test live data coming out of a Flex Service call – FlexUnit re-visited :)

Unit test to test live data coming out of a Flex Service call – FlexUnit re-visited :)

Here is an attempt to explain the way(one of the ways) to write unit tests for a service call(via HTTPService,WebService or RemoteObject) which  returns live data.


1) Adobe Flash Builder4 beta.

2) Please import the project attached here

Note: Refresh the service EmployeeService in the Data/Services panel for the project to compile succesfully

My use-case:

I have an xml file called employees.xml, i would like to display the details of all the employees available in my application.I use HTTPService to get the data to the flex application. Using the Data centric application dvevelopment(DCRAD) wizards i generate a new HTT P Service with an operation called getEmployees( refer to EmployeeService.as and _Super_EmployeeService.as in services folder in the attached project).

My job is to find out if the function getEmployees is returning the right data. So i perform an unit test to check if the number of employees is correct. FYI the getEmployees function returns me an AsyncToken whose result property will be populated with the result of the operation when the server receives the response.

Please find the code for the getEmployees function below:

public function getEmployees() : AsyncToken
var _internal_operation:AbstractOperation = _serviceControl.getOperation(“getEmployees”);
var _internal_token:AsyncToken = _internal_operation.send() ;
return _internal_token;

public function getEmployees() : AsyncToken


var _internal_operation:AbstractOperation = _serviceControl.getOperation(“getEmployees”);

var _internal_token:AsyncToken = _internal_operation.send() ;

return _internal_token;


Using the FlexUnit integration in Flash Builder i create a test case to test the above function.

Please refer to creation of test case class from https://balajisridhar.wordpress.com/2009/06/06/a-jolly-ride-with-flexunit-in-flash-builder4/

In the Test Case wizard, name the class as ‘EmpServiceTest’ and select ‘Class to test’ as EmployeeService.as and select the ‘Method to Test’ as getEmployees.

In the EmpServiceTest class generated by Flash Builder add the following code in the method ‘testGetEmployees’

var asynToken:AsyncToken;

var myTimer:Timer;

asynToken = classToTestRef.getEmployees();

myTimer = new Timer(3000,1);



Please find this code in the EmpServiceTest.as in the flexUnitTests folder in the attached project

There are 2 things which i am performing here:

a) Calling the getEmployees function from the reference(classToTestRef) of EmployeeService.as class.

b) Trying to hold the application untill my tests run by setting the timer to a particular time(3500 s) and calling a function resultHandler1.

Now create a new function resultHandler1 by pasting the following code in ‘EmpServiceTest.as’:

public  function resultHandler1(event:TimerEvent):void



var arrColl:ArrayCollection =asynToken.result as ArrayCollection;


assertEquals(arrColl.length, 26);


I am trying to process the result from the AsyncToken and checking if the number of employees in the result is 26.To do this i assign the asyncToken’s result to an ArrayCollection and i am checking for the length property of the ArrayCollection.

Run the test by context clicking on ‘EmpServiceTest’ and select ‘Execute FlexUnit Tests’. The test passes as the number of employees are 26 and the result is displayed in the Flash Builder’s ‘FlexUnit Results’ panel.


Categories: Uncategorized
  1. Bhuvana
    December 7, 2009 at 1:15 pm

    This blog entry is very useful for me….
    I want to write test case for my application.
    and my code is below.

    var req :mainRequest = new mainRequest();



    public function clearSession():void

    var _remoteHttp : HTTPService = new HTTPService();
    _remoteHttp.url = ApplicationCommonModel.getInstance().serverContextUrl+’logout’;
    _remoteHttp.method = “POST”;
    _remoteHttp.resultFormat = “e4x”;

    ResultEvent.RESULT, function (event:ResultEvent):void{
    } );
    FaultEvent.FAULT, function (event:FaultEvent):void{
    } );


    could u please tel me how write test case for this block of code.?

    Thanks & Regards,

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: