Tuesday, August 18, 2009

Using FlexUnit 4 with Flex 3: Sample Application

We had hard time finding on the internet a sample like this. So, here's ours. Additional steps required for this test to be run:
  • download FlexUnit 4 from their site.
  • add the FlexUnit swc files to the project's classpath
  • copy the sources below into your project and adapt them (use the view source icon in each source code frame below to view/copy the source code).
  • once everything is on place, right click on the TestRunner and select Run Flex Application.
At the end of the article, we have added some notes about using FlexUnit with FlashBuilder 4 and Flex 4
Enjoy! It's a great tool!

File structure

Tested Object

package olcc.account
public class Account
public function Account(){

private var balance:Number=0;

public function deposit(amount:Number): void {

public function withdraw(amount:Number): void {

public function getBalance():Number {
return balance;
Test Suite (Optional)

package olcc.account
import org.flexunit.runners.Suite;

public class MyTestSuite
public var t1: AccountTest;


Unit Test

package olcc.account
import org.flexunit.Assert;

public class AccountTest
public function testNew():void {
var account:Account = new Account();
Assert.assertEquals("Expecting zero account balance", 0, account.getBalance());

public function testDeposit():void {
var account:Account=new Account();
Assert.assertEquals("Balance on a new account after 50 deposit is 50",50,account.getBalance());
Assert.assertEquals("Balance after 50 deposit and another 25 deposit is 75", 75,account.getBalance());


public function testWithdraw():void {
var account:Account = new Account();
Assert.assertEquals("Balance should be: + $100 - $50 = $50",50,account.getBalance());

Test Runner
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

import org.flexunit.runner.FlexUnitCore;
import org.flexunit.flexui.TestRunnerBase;
//Add an import statement(s) for the class(es) under test
import olcc.account.MyTestSuite;

private var core: FlexUnitCore;

private function onCreationComplete():void
core = new FlexUnitCore();

<flexunit:TestRunnerBase id="testRunner" width="100%" height="100%" />

Using FlexUnit 4 with Flex 4

Flex 4 beta2 has support for FlexUnit, and the most recent version of it which is version 4.0. If you go to the New->TestCase menu, FlashBuilder can create for you a whole test class. And add to the flex build path the necessary libraries. This is done pretty well. But when I tried to apply the simple test application presented above to a Flex 4 application, I ran into multiple problems. One critical, as of today, was the issue that the AsDoc for Flex 4 is not available, nor much of the documentation for it. So, some classes from FlexUnit 4 that I needed (like TestRunnerBase), I wasn't able to find. Until these issues are resolved in Flex 4, I decided to use the FlexUnit 4 directly. To do this, I removed the Flex 4 libraries from the build path and added the FlexUnit 4 beta 2 libraries (.swc files). In general, the flex parser is not always stable, which showed up in this exercise and I had a compilation error that shows every other time I compile my TestRunner.mxml (with the same source code), but I can run the tests. Simply, I made sure that the last compilation is a clean one. Also, closing and reopening the project eliminated this issue.


shashi said...

nice tutorial for unit testing of flex application.

Tyler Arehart said...

This was very helpful, thanks!

alen said...

great job guys, the best and only intro on the web for fu4

Sandra said...

Thx for this tutorial, it really helped me!

Ty said...

Anyone else having trouble with the namespace URL?

http://www.adobe.com/2009/flexUnitUIRunner It can't seem to be found for me currently.

DJ Ether said...

Yes I am having this issue. Can't find a solution so far.

Amy B said...

I found that the FlexUnit Turnkey at adobe.com is an fxp, which I suppose means they want you to have Flash Builder. To get the turnkey for Flex Builder, try http://www.flexunit.org/?page_id=14

devilprince said...

good job nice tutorial, but just wanna ask if there is any directory structure for testing files? I'm thinking to put all the test in one place.