A First Date with Unit Testing in Flash Builder 4

Unit testing is a topic that creates a variety of opinions. For a lot of people it is like a first date – the first time they try to take out unit testing for a spin it seems like it is fat, slow, and impedes what you want to get done. For others, it is love at first sight.

To be honest, I still struggle to decide if I want to include unit testing in my relationship with each new project I take on. I know that it is needed, and that it really is best practice, but then I get thoughts such as:

  • “Is unit testing critical to this mission?”
  • “This is just a small project, it really doesn’t need it”
  • “Crap, I don’t like Test Driven Development”

Even though I murmur about the different times to use unit testing, as Flex and Flash keep moving to become bigger players in developing true business applications,  the greater the need to verify an applications functionality.

What’s cool about Flash Builder 4 and unit testing is that we finally have a solution that is actually built into the IDE. It seems that the wars between FlexUnit, FlexMonkey, Structured Log Testing, and AsUnit were won by Flex Unit when they were crowned by Adobe to be integrated in the IDE.

If you need a refresher in what unit tests are and why they are important, check this out.

So let’s take a first date with unit tests in Flash Builder 4.

1) Create a project and create a simple class that will do some business logic we want to test. In this example, we use the standard bank account example that makes deposits and withdrawals.
package
{
public class Bank
{
public function Bank() { }

private var _balance:Number;

public function get balance():Number {
return _balance;
}

public function set balance(v:Number):void {
_balance = v;
}

/**
* Deposit some money into our bank
* @param ammount
* @return
*
*/
public function deposit(ammount:Number):Number {
_balance += ammount;
return balance;
}

/**
* Withdraw some money from our bank
* @param ammount
* @return
*
*/
public function withdraw(ammount:Number):Number {
_balance -= ammount;
return balance;
}
}
}

2) Now it’s really easy to create a test case. Just right click the class you want to test and create a new test case off it:

newTestCase

This will name the class what it should be called (in our case BankTest.as) and insert all of your stub functions to setup/tear down the test case. You will also notice one extra file created in your directory called FlexUnitCompilerApplication. This exists simply to be the application that runs when you execute the test cases. It will also show you a nice pretty dialogue when all your tests run successfully.

3) Configure your test case. Below is a sample:
package flexUnitTests
{
import flexunit.framework.TestCase;

public class BankTest extends TestCase
{
// please note that all test methods should start with 'test' and should be public

// Reference declaration for class to test
private var classToTestRef : Bank;

public function BankTest(methodName:String=null)
{
super(methodName);
}

//This method will be called before every test function
override public function setUp():void
{
super.setUp();
classToTestRef = new Bank();
}

//This method will be called after every test function
override public function tearDown():void
{
super.tearDown();
}

/* sample test method
public function testSampleMethod():void
{
// Add your test logic here
fail("Test method Not yet implemented");
}
*/

/**
* Do a test deposit
*
*/
public function testDesposit():void {

//Reset our balance to zero
classToTestRef.balance = 0;

//An ammount to deposit
var ammount:Number = 1000;

//Deposit the ammount
classToTestRef.deposit(ammount)

//Check to make sure that this deposit posts to the balance successfully
assertEquals(ammount, ammount);
}

/**
* Do a test on a withdrawal
*
*/
public function testWithdrawal():void {

//Reset our balance to zero
classToTestRef.balance = 1000;

//An ammount to withdraw
var ammount:Number = 400;

//What the end ammount of our withdrawal should be
var endAmmount:Number = 600;

//Withdraw the ammount
classToTestRef.withdraw(ammount);

//Check to make sure that this withdrawal posts to the balance successfully
assertEquals(endAmmount, endAmmount);
}
}
}

Now just run your test! See, it’s that easy!

executeTests

If you are successful, you will see two screens with the results of your tests. The first will be in the browser, but the most useful one will be right inside your IDE where you can see which tests executed successfully and which ones did not.

ideTest

So there you go. That was a nice, easy first date with unit tests in Flash Builder 4!

Download the source files.

By: Chase Brammer Categories: Blogroll / debugging / flash / flex

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>