1. loomis
  2. mockito-flex

Wiki

Clone wiki

mockito-flex / releases / Release1.3

Added FlexUnit4 integration:

A sample test with Rule style integration:

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

public class MockingWithFlexUnit4
{
    [Rule]
    public var mockitoRule:IMethodRule = new MockitoRule();

    [Mock(type="acme.TestClass")]
    public var mockie:TestClass;

    public function MockingWithFlexUnit4()
    {
    }

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

MockitoRule ensures all the mock classes are prepared and assigns mock objects.

Old style with a runner:

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

[RunWith("org.mockito.integrations.flexunit4.MockitoClassRunner")]
public class MockingWithFlexUnit4
{
    [Mock(type="acme.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 acme.TestClass;
import org.mockito.integrations.verify;
import org.mockito.integrations.mock;

[Mock(type="acme.TestClass")]

public class MockingWithFlexUnit4
{
   
    [Rule]
    public var mockitoRule:IMethodRule = new MockitoRule();

    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.

You HAVE to reference MockitoClassRunner class in order to get it linked into the unit testing app.

Above sentence is true only if you are using runner. A preferred way for integration is using FlexUnit4 rule

<?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:

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:

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

Updated