Alain Hufkens {Rich Interactive Applications Developer}

3Jun/090

Flex 4: Create a simple Unit Test in Flash Builder

One of the new features of Flash Builder is the support for FlexUnit (the Unit testing framework for Flex applications). You could use FlexUnit in the past, but as from Flash Builder it's a piece of cake to create Test classes for your own code. You just need to follow these simple steps.

NOTE: This demo needs the latest beta version of Flash Builder. Be aware that this is still a beta version and the following could still change in future versions of Flash Builder.

Before we start we need to create a new Flex Project. After creating your project you need to create a class with some functionality that we can use in this example. The typical classes to unit test are classes that have a public interface and have a complicated implementation. When you change something inside the class implementation, the public methods still works as expected. But for the sake of simplicity we will create a very simple class with some simple behaviour.

package net.hufkens
{
  /**
   * Class that does something.
   *
   * @author alain
   *
   */

  public class MyObject
  {
    /**
     * The constructor
     */
 
    public function MyObject()
    {
    }
   
    /**
     * Returns the numeric vallue five
     * @return 5
     */
 
    public function giveMeFive():int
    {
      return 5;
    }
   
    /**
     * Returns the letter A
     * @return "A"
     */
 
    public function sayA():String
    {
      return "B";
    }
   
    /**
     * Returns always true
     * @return true
     */
 
    public function alwaysTrue():Boolean
    {
      return true;
    }
  }
}

Alright, now we have a Class that we can test. To create a new Unit Test you select New > TestCase Class and the following window should appear:

New Test Case Class

Enter the Name of the TestCase Class (in this case: MyTestClass), select the Class to test (in this case: MyObject) and select Finish. Flash Builder now generates several items. First a new folder name flexUnitTests is generated. Also a new mxml file named flexUnitCompilerApplication.mxml is generated. This file is necessary because the Unit Tests need a swf application that can be started and the tests can be executed. The last and most important file that's been generated is the TestCase itself. The only thing you now need to do is implement the actual test methods. In the following code example the test methods are implemented:

package flexUnitTests
{
  import flexunit.framework.Assert;
  import flexunit.framework.TestCase;
 
  import net.hufkens.MyObject;

  public class MyTestClass extends TestCase
  {
    // Reference declaration for class to test
    private var classToTestRef : net.hufkens.MyObject;
   
    public function MyTestClass(methodName:String=null)
    {
      super(methodName);
    }
   
    //This method will be called before every test function
    override public function setUp():void
    {
      super.setUp();
      classToTestRef = new MyObject();
    }
   
    //This method will be called after every test function
    override public function tearDown():void
    {
      super.tearDown();
    }
   
    public function testGiveMeFive():void
    {
      Assert.assertEquals(classToTestRef.giveMeFive(), 5);
    }
   
    public function testSayA():void
    {
      Assert.assertEquals(classToTestRef.sayA(), "A");
    }
   
    public function testIsTrue():void
    {
      Assert.assertTrue(classToTestRef.alwaysTrue());
    }

  }
}

If we now right-click the project and select Execute FlexUnit Tests we get the following message:

FlexUnit warning message

It says we need to create a Test Suite, otherwise Flash Builder can't execute the tests. To create a new Test Suite you select New > TestSuite Class and the following window should appear:

New TestSuite Class

Now select Execute FlexUnit Tests again and your Test are executed. As you can see, one of the tests has failed. A quick view in the source of sayA() method reveals the problem. Fix the problem and all the tests should succeed now.

Unit Test Results

That's all there is to it. Flash Builder makes it a lot easier to create Unit Tests for your libraries and components. This is a good thing, so you should definitely check it out.

  • email
  • Add to favorites
  • RSS
  • Twitter
  • Facebook
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • LinkedIn
  • Google Bookmarks
  • Live
  • Netvibes
  • Technorati
  • Posterous
Filed under: flex Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.