Skip navigation
Jessica Shizuku
Currently Being Moderated

how to access stage in flexunit

Jan 10, 2011 5:11 AM

I've found in the following post ( some suggestions about this topic, but as I'm relatively new to as3, I'm still confused about how to make it work. My team has an as3 only project, and we refer to the stage very often this way:


package clipper{


     public function init() {





     private function onInit() {

          this.stage.frameRate = 24;






Everytime when my testcases call statements like this, it'll give error saying "null object reference", etc. As far as I've read in the other post, I should initiate a VisualTestEnvironment in the test runner, and use the UIImpersonator in the test cases, am I right? If I wanna write a testcase for function init as below, how shall I modify the codes? What shall I add in the mxml test runner? Thanks. I'm building and running the tests with ant.


var _clip:clipper;



public function setUp():void {

     _clip = new clipper();





public function testInit():void{




  • Currently Being Moderated
    Jan 10, 2011 8:52 AM   in reply to Jessica Shizuku

    Not sure if you are using Flash Builder or another tool. First thing, when you are running your tests, you need to provide FlexUnit a reference to the stage:


    It would look something like this:


    core = new FlexUnitCore();

    core.visualDisplayRoot = this;


    /// listeners or other junk

  MyTests );


    In AS3 you can say this.stage inside of any visual class such as a movie clip or sprite that is on the stage, but only visual classes get this property. Since FlexUnit tests aren't visual, you don't get that benefit. The reason you are getting the null error is that the visualDisplayRoot property is not being set.



    Mark as:
  • Currently Being Moderated
    Feb 29, 2012 10:12 AM   in reply to Jessica Shizuku

    I found this BDD project page, which includes a stage access example:




    After much research and complication, I ended up using a Singleton that provides a stage reference that was passed in from a `creationComplete` handler in my TestRunner.




    <?xml version="1.0" encoding="utf-8"?>

    <mx:Application xmlns:mx=""




        layout="absolute" height="800" width="1000">



                import org.flexunit.runner.FlexUnitCore;

                private var core:FlexUnitCore;

                public function onCreationComplete():void {

                    core = new FlexUnitCore();

                    core.addListener( new TraceListener() );

                    core.addListener( new UIListener( uiListener ));

                    new StageLocator(systemManager.stage);






            Application {

                   backgroundColor: #3872b2;

                   backgroundGradientColors: #3872b2, #0c1a3d;

                   backgroundGradientAlphas: 1, 1;

                   themeColor: #ffffff;

                   color: #444444;

                   fontFamily: "Myriad Pro Semibold";

                   fontSize: 12;



        <flexUnitUIRunner:TestRunnerBase id="uiListener" width="100%" height="100%" />




    package {

    import flash.display.Stage;

    public class StageLocator {

        public static var instance:StageLocator;

        public static var stage:Stage;

        public function StageLocator ($stage:Stage) {

            instance = this;

            stage = $stage;




    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points