Wiki

Clone wiki

mockito-flex / tutorials / Integrations

Flex Unit integration

To write unit tests for use with flexunit please extend the MockitoTestCase.

It provides you a convenience constructor where you specify classes to mock.

Let's see how this works:

public class TestMyFirstMockitoTest extends MockitoTestCase
{
    public function TestMyFirstMockitoTest()
    {
         super([Dependency]);
    }
}

The sample test would look like:

public class TestMyFirstMockitoTest extends MockitoTestCase
{
    public function TestMyFirstMockitoTest()
    {
         super([Dependency]);
    }

    public function test():void
    {
         var dependency:Dependency = Dependency(mock(Dependency));
         // given
         var sut:Sut = new Sut(dependency);
         // when
         sut.testedFunction(10);
         // then
         verify().that(dependency.someOperation(any()));
    }
}

Integration test case provides all needed methods for convenient work:

verify(), given(), times(), never(), argThat(), eq(), any(), etc.

Few words on class preparation. Due to the asynchronous nature of the bytecode generation, the integration test case needs to call the runWithResult() as a callback after receiving notification about the generated classes. There is another problem related to the byte code generation which is that the player requires special permissions to enable byte code loading. You can notice that when you execute your tests, the swf in the browser is reloaded when the mock classes are ready. This means youwon't be easily able to debug your tests as the reloaded instance is no longer connected to the debugger.

For more details you may need to check Asmock documentation.

AS Unit integration

James came up with the AS Unit integration. For now it's available on his blog. I'll add it to the framework soon.

http://blog.hoardinghopes.com/index.php/2009/07/mockito-flex-meets-asunit/

Flex Unit 4 integration

It will come soon.

Updated