Using custom runners with Flash Builder 4's built-in FlexUnit4 support

Recently, I’ve been trying to get mockito-flex to play nice with Flash Builder 4’s built-in FlexUnit4 support. For its part, mockito requires the use of a custom test runner class, MockitoClassRunner, to run any test case containing mockito dependencies, declared using FlexUnit4’s “RunWith” metadata tag:

public class MockitoTestCase

One caveat of custom runners is that they require being linked into the project:

– Used to identify a specific runner that should be used for this class. Common uses include specifying a class to use the Suite runner or Theory runner. The class specified must be linked into the project and must implement the IRunner interface.


Without proper linkage FlexUnit4 will not be able to find the runner, throwing a runtime error such as the following:

Error: Custom runner class org.mockito.integrations.flexunit4.MockitoClassRunner should be linked into project and implement IRunner. Further it needs to have a constructor which either just accepts the class, or the class and a builder.

A simple way of linking the runner is to include a variable typed to the custom runner’s class inside of the class that initiates the test run. When using FB4’s built-in FlexUnit4 support, the class running the tests is FlexUnitApplication, auto-generated by FB4 after tests have been selected in the “Run FlexUnit Tests” dialog.

public class FlexUnitApplication extends Sprite
    private var mockitoRunner:MockitoClassRunner;

    public function FlexUnitApplication()

Though the auto-generation of FlexUnitApplication is very convenient feature, automating the otherwise tedious, manual labor of supplying test cases to the runner’s “run()” method, its re-generated from scratch each time the test case selection changes, discarding any custom alterations, including the linkage variable. Manually adding the variable each time FlexUnitApplication is regenerated is a tedious and error-prone process in itself, but one that can thankfully be avoided using the following technique.

In FB4’s “Preferences” dialog, the one and only FlexUnit specific option lends itself very well to this situation. Here you can declare a custom application for FlexUnit4 to run instead of the one that’s auto-generated. Do note that using a custom application class does not stop the auto-generation of FlexUnitApplication, a fact that can be utilized to add a linkage variable that won’t get thrown away each time FlexUnitApplication is regenerated.

After a custom application name has been defined, the corresponding class needs to be created somewhere within the project hierarchy. This class should inherit from FlexUnitApplication and contain the linkage variable:

import org.mockito.integrations.flexunit4.MockitoClassRunner;

public class CustomFlexUnitApplication extends FlexUnitApplication
    private var mockitoRunner:MockitoClassRunner;

    public function CustomFlexUnitApplication() 

Additionally, the custom application class needs to be made one of the runnable application files within the project (else FB4 will let you know so):

Now, when launching the “FlexUnit Tests” application via FB4, CustomFlexUnitApplication will be launched instead of FlexUnitApplication. As the former inherits from the latter, all of the auto-generated convenience is still available while keeping the linkage variable necessary to run the MockitoClassRunner.