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.
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 {
balance=balance+amount;
}
public function withdraw(amount:Number): void {
balance=balance-amount;
}
public function getBalance():Number {
return balance;
}
}
}
Test Suite (Optional)package olcc.account
{
import org.flexunit.runners.Suite;
[Suite]
[RunWith("org.flexunit.runners.Suite")]
public class MyTestSuite
{
public var t1: AccountTest;
}
}
Unit Test
package olcc.account
{
import org.flexunit.Assert;
public class AccountTest
{
[Test]
public function testNew():void {
var account:Account = new Account();
Assert.assertEquals("Expecting zero account balance", 0, account.getBalance());
}
[Test]
public function testDeposit():void {
var account:Account=new Account();
account.deposit(50);
Assert.assertEquals("Balance on a new account after 50 deposit is 50",50,account.getBalance());
account.deposit(25);
Assert.assertEquals("Balance after 50 deposit and another 25 deposit is 75", 75,account.getBalance());
}
[Test]
public function testWithdraw():void {
var account:Account = new Account();
account.deposit(100);
account.withdraw(50);
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"
xmlns="*"
xmlns:flexunit="http://www.adobe.com/2009/flexUnitUIRunner"
creationComplete="onCreationComplete()">
<mx:Script>
<![CDATA[
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();
core.addListener(testRunner);
core.run(MyTestSuite);
}
]]>
</mx:Script>
<flexunit:TestRunnerBase id="testRunner" width="100%" height="100%" />
</mx:Application>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.
