org.as2lib.core.BasicClass +--org.as2lib.test.mock.MockControl
MockControl
is the central class of the mock object framework. You use
it to create your mock object, set expectations and verify whether these
expectations have been met.
The normal workflow is creating a mock control for a specific class or
interface, receiving the mock object from it, setting expectations, setting the
behavior of the mock object, switching to replay state, using the mock object as
if it were a normal instance of a class and verifying that all expectations have
been met.
import org.as2lib.test.mock.MockControl;
// create mock control for class MyClass
var myMockControl:MockControl = new MockControl(MyClass);
// receive the mock object (it is in record state)
var myMock:MyClass = myMockControl.getMock();
// expect a call to the setStringProperty-method with argument 'myString'.
myMock.setStringProperty("myString");
// expect calls to the getStringProperty-method
myMock.getStringProperty();
// return 'myString' for the first two calls
myMockControl.setReturnValue("myString", 2);
// throw MyException for any further call
myMockControl.setDefaultThrowable(new MyException());
// switch to replay state
myMockControl.replay();
// the class under test calls these methods on the mock
myMock.setStringProperty("myString");
myMock.getStringProperty();
myMock.getStringProperty();
// verify that all expectations have been met
myMockControl.verify();
If an expectation has not been met an AssertionFailedError will be
thrown. If an expectation violation is discovered during execution an
AssertionFailedError
will be thrown immediately.
If you had called the setStringProperty
method in the above example
with another string like "unexpectedString"
an AssertFailedError
would have been thrown immediately. If you had called the setStringProperty
method a second time, what has not been expected, an AssertionFailedError
would also have been thrown immediately. If you had not called the
setStringProperty
method at all, an AssertionFailedError
would
have been thrown on verification.
new MockControl()
static public function getDefaultArgumentsMatcher(Void):DefaultArgumentsMatcher
Returns a new default arguments matcher.
a new default arguments matcher
static public function getTypeArgumentsMatcher(expectedTypes:Array):TypeArgumentsMatcher
Returns a new type arguments matcher that is configured with the passed-in
expectedType
.
Type arguments matcher matches arguments by type and not by value.
a type arguments matcher
public function setHandleToStringInvocations(handleToStringInvocations:Boolean):Void
Sets whether to handle toString
invocations on mocks or not.
Handling toString
invocations means that these invocations are
added to the expected or actual behavior. This means if you set
handleToStringInvocations
to true
calling this method on the
mock in replay state results in an added expection and in record state in a
verification whether the call was expected. If you set it to false
the
result of an invocation of the mock's toString
method is returned.
If handleToStringInvocations
is null
, it is interpreted as
false
.
handleToStringInvocations | determines whether to handle toStirng
method invocations
|
public function areToStringInvocationsHandled(Void):Boolean
Returns whether toString
invocations on the mock are handled.
Handling toString
invocations means that these invocations are
added to the expected or actual behavior. This means if they are handled,
calling the toString
method on the mock in replay state results in an
added expection and in record state in a verification whether the call was
expected. If they are not handled, the result of an invocation of the mock's
toString
method is returned.
true
if toString
invocations are handled else
false
public function getMockProxyFactory(Void):ProxyFactory
Returns the currently used mock proxy factory.
This proxy factoy is either the default TypeProxyFactory or the one
set via setMockProxyFactory
.
the currently used proxy factory
public function setMockProxyFactory(proxyFactory:ProxyFactory):Void
Sets the proxy factory used to obtain the mock proxis / mocks.
If proxyFactory
is null
the getMockProxyFactory
method will use the default factory.
proxyFactory | factory to obtain mock proxies / mocks |
public function getRecordStateFactory(Void):MockControlStateFactory
Returns the currently used record state factory.
This is either the factory set via setRecordStateFactory
or the
default one, which returns instances of the RecordState class.
the currently used record state factory
public function setRecordStateFactory(recordStateFactory:MockControlStateFactory):Void
Sets the new record state factory.
If recordStateFactory
is null
the default record state
factory gets returned by the getRecordStateFactory
method.
recordStateFactory | the new record state factory |
public function getReplayStateFactory(Void):MockControlStateFactory
Returns the currently used replay state factory.
This is either the factory set via setReplayStateFactory
or the
default one, which returns instances of the ReplayState class.
the currently used replay state factory
public function setReplayStateFactory(replayStateFactory:MockControlStateFactory):Void
Sets the new replay state factory.
If replayStateFactory
is null
the
getReplayStateFactory
method will return the default replay state
factory.
replayStateFactory | the new replay state factory |
public function getMock(Void)
Returns the mock object.
The mock can be casted and typed to the interface or class specified on instantiation.
The mock is created using the mock proxy factory returned by the getMockProxyFactory method.
Once the mock object has been created it is cached. That means this method always returns the same mock object for this mock control.
the mock object
public function replay(Void):Void
Switches the mock object from record state to replay state.
The mock object is in record state as soon as it gets returned by the getMock method.
You cannot record expectations in replay state. In replay state you verify that all your expectations have been met, by using the mock as it were a real instance.
If an expectations is not met an AssertionFailedError is thrown.
This is either done during execution of your test or on verification. Take a
look at the example provided in the class documentation to see when what
AssertFailedError
is thrown.
public function reset(Void):Void
Resets the mock control and the mock object to the state directly after creation.
That means that all previously made expectations will be removed and that the mock object will be again in record state.
public function setArgumentsMatcher(argumentsMatcher:ArgumentsMatcher):Void
Sets the arguments matcher that will be used for the last method specified by a method call.
argumentsMatcher | the arguments matcher to use for the specific method |
IllegalStateException | if this mock control is in replay state |
public function setDefaultReturnValue(value):Void
Records that the mock object will by default allow the last method specified by a method call and will react by returning the provided return value.
Default means that the method can be called 0 to infinite times without expectation errors.
value | the return value to return |
IllegalStateException | if this mock control is in replay state |
public function setDefaultThrowable(throwable):Void
Records that the mock object will by default allow the last method specified by a method call, and will react by throwing the provided throwable.
Default means that the method can be called zero to infinite times without expectation errors.
throwable | the throwable to throw |
IllegalStateException | if this mock control is in replay state |
public function setDefaultVoidCallable(Void):Void
Recards that the mock object will by default allow the last method specified by a method call.
Default means that the method can be called zero to infinite times without expectation errors.
Calling this method is not necessary. The mock control expects the last method specified by a method call as soon as this method call occured.
IllegalStateException | if this mock control is in replay state |
public function setReturnValueByValue(value):Void
Records that the mock object will expect the last method call once and will react by returning the provided return value.
value | the return value to return |
IllegalStateException | if this mock control is in replay state |
public function setReturnValueByValueAndQuantity(value, quantity:Number):Void
Records that the mock object will expect the last method call a fixed number of times and will react by returning the provided return value.
value | the return value to return |
quantity | the number of times the method is allowed to be invoked |
IllegalStateException | if this mock control is in replay state |
public function setReturnValueByValueAndMinimumAndMaximumQuantity(value, minimumQuantity:Number, maximumQuantity:Number):Void
Records that the mock object will expect the last method call between
minimumQuantity
and maximumQuantity
times and will react by
returning the provided return value.
value | the return value to return |
minimumQuantity | the minimum number of times the method must be called |
maximumQuantity | the maximum number of times the method can be called |
IllegalStateException | if this mock control is in replay state |
public function setThrowableByThrowable(throwable):Void
Records that the mock object will expect the last method call once and will react by throwing the provided throwable.
throwable | the throwable to throw |
IllegalStateException | if this mock control is in replay state |
public function setThrowableByThrowableAndQuantity(throwable, quantity:Number):Void
Records that the mock object will expect the last method call a fixed number of times and will react by throwing the provided throwable.
throwable | the throwable to throw |
quantity | the number of times the method is allowed to be invoked |
IllegalStateException | if this mock control is in replay state |
public function setThrowableByThrowableAndMinimumAndMaximumQuantity(throwable, minimumQuantity:Number, maximumQuantity:Number):Void
Records that the mock object will expect the last method call between
minimumQuantity
and maximumQuantity times
and will react by
throwing the provided throwable.
throwable | the throwable to throw |
minimumQuantity | the minimum number of times the method must be called |
maximumQuantity | the maximum number of times the method can be called |
IllegalStateException | if this mock control is in replay state |
public function setVoidCallableByVoid(Void):Void
Records that the mock object will expect the last method call once and will react by returning silently.
IllegalStateException | if this mock control is in replay state |
public function setVoidCallableByQuantity(quantity:Number):Void
Records that the mock object will expect the last method call a fixed number of times and will react by returning silently.
quantity | the number of times the method is allowed to be invoked |
IllegalStateException | if this mock control is in replay state |
public function setVoidCallableByMinimumAndMaximumQuantity(minimumQuantity:Number, maximumQuantity:Number):Void
Records that the mock object will expect the last method call between
minimumQuantity
and maximumQuantity
times and will react by
returning silently.
minimumQuantity | the minimum number of times the method must be called |
maximumQuantity | the maximum number of times the method can be called |
IllegalStateException | if this mock control is in replay state |