loomis / mockito-flex

simpler & better mocking for flex

Clone this repository (size: 12.3 MB): HTTPS / SSH
$ hg clone http://bitbucket.org/loomis/mockito-flex/

Added FlexUnit4 integration:

A sample test:

import org.mockito.TestClass;
import org.mockito.integrations.verify;

[RunWith("org.mockito.integrations.flexunit4.MockitoClassRunner")]
public class MockingWithFlexUnit4
{
    [Mock(type="org.mockito.TestClass")]
    public var mockie:TestClass;

    public function MockingWithFlexUnit4()
    {
    }

    [Test]
    public function shouldVerifyMockInvocation():void
    {
        // when
        mockie.argumentless();
        // then
        verify().that(mockie.argumentless());
    }
}

To automatically prepare and assign mocks a metadata Mock should be placed over a public field of your test class. Mockito will try to guess type from a field if you don't provide a type parameter. If for any reason mock type is different than the field type need to provide mocked class name via type parameter of the Mock metadata. Additionally you can provide a mock name via name parameter as well as the constructor arguments if required via argsList that should be a name of the public field of the test class that holds an Array of the required parameters.

For the cases when you want to have full control over mock creation process, you should specify list of [Mock(type="...")] metadata for the test class in order for Mockito to prepare the mock classes and then create mock objects in a convenient place using mock() function:

import org.mockito.TestClass;
import org.mockito.integrations.verify;
import org.mockito.integrations.mock;

[Mock(type="org.mockito.TestClass")]

[RunWith("org.mockito.integrations.flexunit4.MockitoClassRunner")]
public class MockingWithFlexUnit4
{
   
    public var mockie:TestClass;

    public function MockingWithFlexUnit4()
    {
    }

    [Test]
    public function shouldVerifyMockInvocation():void
    {
        // given
        mockie = mock(TestClass);
        // when
        mockie.argumentless();
        // then
        verify().that(mockie.argumentless());
    }
}

Sample runner application if you have problems finding one. It's not required but you HAVE to reference MockitoClassRunner class in order to get it linked into the unit testing app.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                minWidth="1024" minHeight="768"
                xmlns:flexUnitUIRunner="http://www.adobe.com/2009/flexUnitUIRunner"
                xmlns:flexunit="flexunit.flexui.*"
                xmlns:flexunitRunner="org.flexunit.runner.*" creationComplete="startupTests()">
    <mx:Script>
     <![CDATA[
        import org.flexunit.listeners.UIListener;
        import org.flexunit.runner.FlexUnitCore;
        import org.mockito.integrations.flexunit4.MockitoClassRunner;

        private var core:FlexUnitCore = new FlexUnitCore();

        private function startupTests():void
        {
            core.addListener(new UIListener(uiListener));
            core.run(put your test classes/suites here);
        }

        private var requiredImportForTheIntegrationToWork:MockitoClassRunner;
     ]]>
    </mx:Script>

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

Added ordered verification:

1
2
inOrder().verify().that(someone.firstFunction());
inOrder().verify().that(someone.secondFunction());

Upgraded asmock to version 0.9. Asmock is now compiled in the swc to allow simple maven management.

Added support for calling original function for stubs:

1
given(someone.something(any())).will(callOriginal());

This revision is from 2010-03-03 23:07