1 Reply Latest reply on Apr 3, 2012 4:01 AM by Walter Treur

    Spark & UIImpersonator


      FlexUnit 4.1

      FlashBuilder 4.5.1


      I'm trying to test a custom Flex 4 skinnable component, using the FlexUnit UIImpersonator class. If I run my tests from a FlashBuilder Spark only project everything works fine. If I try to test from a project with the mx component set on the buildpath I get a "getElementIndex not available in non Flex 4 projects" error.


      Can I unit test spark components in FlexUnits visual test environment while still having the mx component set on the classpath?


      UIImpersonator delegates it's method calls to a "testEnvironment". The implementation used for this "testEnvironment" is decided by the VisualTestEnvironmentBuilder class and the FlexEnvironmentBuilder class. If the FlexEnvironmentBuilder class can find the "mx.core.Container" on the classpath it returns a MX environment, else a Spark environment. Only the spark environment has valid implementations for Flex 4 relevant method calls on the UIImpersonator – like the addElement method.

        • 1. Re: Spark & UIImpersonator
          Walter Treur

          I've had the same issue as well and I think it's a bug in FlexUnit.


          Here's my workaround which I've also added to the same question you've asked at stackoverflow:


          As a workaround. Just use UIImpersonator.addChild() and add a reference to mx.core.Container in your TestRunner. This ensures that a MX container is used as testEnvironment and you won't receive a 'getElementIndex...' error.


          import mx.core.Container


          public function runTests():void {

              // reference to mx container

              var containerRef:Container = new Container();



              // Run your testsuite as usual. eg:

              var core:FlexUnitCore = new FlexUnitCore();