Mockito-Flex meets ASUnit
Well, that’s not a catchy headline, but it does pretty much sum it up. I’ve been playing with Mockito-Flex, a mock object framework for Actionscript 3. It’s great, and made vastly easier to pick up by a Mockito-FlexUnit bridge in the form of a MockitoTestCase that does the hard work of reporting the mocking results and validations in a way that FlexUnit can display.
Of course, FlexUnit does require the Flex framework, which bulks up the whole thing. Oh, and I’m used to using ASUnit. So I’ve written the following class that does the same job as MockitoTestCase, but for ASUnit:
-
/**
-
* The MIT License
-
*
-
* Copyright (c) 2009 Mockito contributors
-
*
-
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
-
* and associated documentation files (the "Software"), to deal in the Software without restriction,
-
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
-
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
-
* subject to the following conditions:
-
*
-
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
*
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
-
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
-
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
-
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
*/
-
-
package org.mockito {
-
import asunit.framework.TestCase;
-
-
import org.mockito.api.Matcher;
-
import org.mockito.api.MethodSelector;
-
import org.mockito.api.MockCreator;
-
import org.mockito.api.Stubber;
-
import org.mockito.api.Verifier;
-
-
-
public class ASUnitMockitoTestCase extends TestCase {
-
-
private var _mockClasses:Array;
-
-
protected var mockito:Mockito;
-
-
public function ASUnitMockitoTestCase(mockClasses:Array, testMethod:String = null)
-
{
-
_mockClasses = mockClasses;
-
super(testMethod);
-
}
-
-
/**
-
* Due to the asynchronous nature of the class generation
-
* a test needs to execute from a callback function
-
*/
-
public override function run():void
-
{
-
if (mockito == null && _mockClasses)
-
{
-
mockito = new Mockito();
-
var superRun:Function = super.run;
-
mockito.prepareClasses(_mockClasses, repositoryPreparedHandler);
-
function repositoryPreparedHandler():void
-
{
-
superRun();
-
}
-
}
-
else
-
{
-
super.run();
-
}
-
}
-
-
-
-
/**
-
* Constructs mock object
-
* @param clazz a class of the mock object
-
* @param constructorArgs constructor arguments required to create mock instance
-
* @param name a name used in various output
-
* @return a mocked object
-
*/
-
public function mock(classToMock:Class, name:String = null, constructorArgs:Array = null):Object
-
{
-
return mockito.mock(classToMock, name, constructorArgs);
-
}
-
-
/**
-
* A starter function for verification of executions
-
* If you dont specify the verifier, an equivalent of times(1) is used.
-
* @param verifier object responsible for verification of the following execution
-
*/
-
public function verify(verifier:Verifier = null):MethodSelector
-
{
-
return mockito.verify(verifier);
-
}
-
-
/**
-
* A starter function for stubbing
-
* @param methodCallToStub call a method to stub as an argument
-
* @return an object providing stubbing options
-
*/
-
public function given(methodCallToStub:*):Stubber
-
{
-
return mockito.given(methodCallToStub);
-
}
-
-
/**
-
* @private
-
*/
-
protected function get mockCreator():MockCreator
-
{
-
return mockito;
-
}
-
-
/**
-
* Matches any argument including <code>null</code>
-
*/
-
public function any():*
-
{
-
return mockito.any();
-
}
-
-
/**
-
* Equality matcher
-
* Example:
-
* <listing>
-
* verify(never()).that(system.login(eq("root")));
-
* </listing>
-
*/
-
public function eq(expected:*):*
-
{
-
return mockito.eq(expected);
-
}
-
-
/**
-
* A fluent interface for making sure call hasn't happened
-
* Example:
-
* <listing>
-
* verify(never()).that(operator.execute());
-
* </listing>
-
*/
-
public function never():Verifier
-
{
-
return mockito.never();
-
}
-
-
/**
-
* A fluent interface for counting calls
-
* Example:
-
* <listing>
-
* verify(times(2)).that(operator.execute());
-
* </listing>
-
*/
-
public function times(expectedCallsCount:int):Verifier
-
{
-
return mockito.times(expectedCallsCount);
-
}
-
-
/**
-
* A fluent interface for custom matcher
-
* Example:
-
* <listing>
-
* verify().that(system.login(argThat(new HashOnlyCapitalLettersMatcher())));
-
* </listing>
-
*
-
* A good practice is to create a matcher recording function somewhere and name it
-
* after the matcher. It's important to return a wildcard from the function to let it
-
* work with any arugment of the function
-
* <listing>
-
* function hasOnlyCapitalLetters():*
-
* {
-
* argThat(new HashOnlyCapitalLettersMatcher());
-
* }
-
* </listing>
-
*/
-
public function argThat(matcher:Matcher):*
-
{
-
return mockito.argThat(matcher);
-
}
-
}
-
}
I’ve built this against ASUnit3, and it appears to be running fine. As you can see, it’s release under the MIT license, as is Mockito-Flex. I’d appreciate any feedback that’s going, so have a play and let me know …
Kris — July 31, 2009 @ 8:53 am
James,
Great work! I'll add it to the integrations in the tutorial.
Regards,
Kris
Luke Bayes — July 31, 2009 @ 7:31 pm
Thanks James –
This looks great!