Alain Hufkens {Rich Interactive Applications Developer}

30Jul/098

Testing PureMVC code with FlexUnit

The PureMVC FlexUnit Testing project is already quite old (last update was June 2008), but I just discovered it because I am currently working on a big Adobe AIR project that currently does not have a User Interface yet. We use the PureMVC framework for most of our bigger applications, and because it depends on Notifications it's not that easy to Unit Test. Proxies don't return results synchronously, but send notifications with the result in the body.

Well this small library helps connecting your PureMVC implementation code to the FlexUnit framework. The following example code shows the code for a Test Class for a DataProxy that has a method getSomeData(). As you can see, you need to write some code to access the facade, proxy, ... but it's all pretty straight forward.

package unitTests
{
  import net.hufkens.example.ApplicationFacade;
  import net.hufkens.example.config.ApplicationNotifications;
  import net.hufkens.example.model.DataProxy;

  import com.andculture.puremvcflexunittesting.*;

  import org.puremvc.as3.core.View;
  import org.puremvc.as3.interfaces.IView;

  public class DataProxyTest extends PureMVCTestCase
  {
    private var timeout:int = 0;

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

    protected function get facade():ApplicationFacade
    {
      return ApplicationFacade.getInstance();
    }

    public override function setUp():void
    {
      facade.registerProxy(new DataProxy());
    }

    protected function get proxy():DataProxy
    {
      var proxy:DataProxy =
      DataProxy(facade.retrieveProxy(DataProxy.NAME));
      return proxy;
    }

    protected function get view():IView
    {
      return View.getInstance() as IView;
    }

    public function testGetSomeData():void
    {
      registerObserver(view, proxy,
        ApplicationNotifications.GET_SOME_DATA_DONE,
        response, timeout);

      proxy.getSomeData();
    }

    private function response(e:PureMVCNotificationEvent):void
    {
      assertTrue("data is not available",
      e.notification.getBody().data.length>0);
    }
  }
}

It worked great for this project. I hope it will still be supported for Flex 4 so we can use the built-in unit testing features of Flash Builder.

Make sure you checkout the latest version from Google Code, because the swc file has an issue with multicore namespaces. The latest version in svn works fine.

Filed under: air, flex 8 Comments
15Jul/095

FlexMonkey 1.0 released (adding AIR support)

gorillalogoThis week the guys from Gorilla Logic released FlexMonkey version 1.0 beta. I have been testing the previous version on one of my Flex applications and now that they have released it as an AIR application I will definitely start using it more and more for future projects.

So, what exactly is it?

FlexMonkey is an Adobe AIR application used for testing Flex- and AIR-based applications.  Providing the functionality to record, playback and verify Flex UI interactions, FlexMonkey also generates ActionScript-based testing scripts that you can easily include within a continuous integration environment.

How does it help a developer like me build better applications? Well, you need to test your application right, and in the end you always end up doing the same actions over and over again. FlexMonkey can record those repetitive actions so you can replay this if you need to test. From those recorded actions, FlexMonkey can generate AS3 code that you can add to your project. And if you use some kind of continuous integration solution (like Cruise Control, Hudson, ...), these test cases can be plugged in and automatically executed. FlexMonkey supports Fluint and FlexUnit 4 for unit test creation and has support for Ant and Hudson. The source code is available on Google Code.

You can read more about testing your Flex application in this article on Adobe Developer Connection or check out this introduction video. It should give you an impression of what the application can do.

Congratz guys and keep up the good work!

Filed under: air, flex 5 Comments
3Jun/094

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.

Filed under: flex 4 Comments