Access keys

MockControl

Kind of class: class
Inherits from: BasicClass
Author: Simon Wacker
Classpath: org.as2lib.test.mock.MockControl
File last modified: Wednesday, 17 August 2005, 15:18:52
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.

Class methods

getDefaultArgumentsMatcher

static function getDefaultArgumentsMatcher (
Returns a new default arguments matcher.
Returns:
a new default arguments matcher

getTypeArgumentsMatcher

static 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.

Returns:
a type arguments matcher

Instance methods

areToStringInvocationsHandled

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.

Returns:
true if toString invocations are handled else
false

getMock

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.

Returns:
the mock object

getMockProxyFactory

function getMockProxyFactory (
Void) : ProxyFactory
Returns the currently used mock proxy factory.

This proxy factoy is either the default org.as2lib.env.reflect.TypeProxyFactory or the one
set via setMockProxyFactory.

Returns:
the currently used proxy factory

getRecordStateFactory

function getRecordStateFactory (
Returns the currently used record state factory.

This is either the factory set via setRecordStateFactory or the
default one, which returns instances of the org.as2lib.test.mock.support.RecordState class.

Returns:
the currently used record state factory

getReplayStateFactory

function getReplayStateFactory (
Returns the currently used replay state factory.

This is either the factory set via setReplayStateFactory or the
default one, which returns instances of the org.as2lib.test.mock.support.ReplayState class.

Returns:
the currently used replay state factory

replay

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.

reset

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.

setArgumentsMatcher

function setArgumentsMatcher (
argumentsMatcher:ArgumentsMatcher) : Void
Sets the arguments matcher that will be used for the last method specified by
a method call.
Parameters:
argumentsMatcher:
the arguments matcher to use for the specific method
Throws:
org.as2lib.env.except.IllegalStateException if this mock control is in replay state

setDefaultReturnValue

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.

Parameters:
value:
the return value to return
Throws:
org.as2lib.env.except.IllegalStateException if this mock control is in replay state

setDefaultThrowable

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.

Parameters:
throwable:
the throwable to throw
Throws:
org.as2lib.env.except.IllegalStateException if this mock control is in replay state

setDefaultVoidCallable

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.

Throws:
org.as2lib.env.except.IllegalStateException if this mock control is in replay state

setHandleToStringInvocations

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.

Parameters:
handleToStringInvocations:
determines whether to handle toStirng
method invocations

setMockProxyFactory

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.

Parameters:
proxyFactory:
factory to obtain mock proxies / mocks

setRecordStateFactory

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.

Parameters:
recordStateFactory:
the new record state factory

setReplayStateFactory

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.

Parameters:
replayStateFactory:
the new replay state factory

setReturnValueByValue

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.
Parameters:
value:
the return value to return
Throws:
org.as2lib.env.except.IllegalStateException if this mock control is in replay state

setReturnValueByValueAndMinimumAndMaximumQuantity

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.
Parameters:
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
Throws:
org.as2lib.env.except.IllegalStateException if this mock control is in replay state

setReturnValueByValueAndQuantity

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.
Parameters:
value :
the return value to return
quantity:
the number of times the method is allowed to be invoked
Throws:
org.as2lib.env.except.IllegalStateException if this mock control is in replay state

setThrowableByThrowable

function setThrowableByThrowable (
throwable) : Void
Records that the mock object will expect the last method call once and will
react by throwing the provided throwable.
Parameters:
throwable:
the throwable to throw
Throws:
org.as2lib.env.except.IllegalStateException if this mock control is in replay state

setThrowableByThrowableAndMinimumAndMaximumQuantity

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.
Parameters:
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
Throws:
org.as2lib.env.except.IllegalStateException if this mock control is in replay state

setThrowableByThrowableAndQuantity

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.
Parameters:
throwable:
the throwable to throw
quantity :
the number of times the method is allowed to be invoked
Throws:
org.as2lib.env.except.IllegalStateException if this mock control is in replay state

setVoidCallableByMinimumAndMaximumQuantity

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.
Parameters:
minimumQuantity:
the minimum number of times the method must be called
maximumQuantity:
the maximum number of times the method can be called
Throws:
org.as2lib.env.except.IllegalStateException if this mock control is in replay state

setVoidCallableByQuantity

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.
Parameters:
quantity:
the number of times the method is allowed to be invoked
Throws:
org.as2lib.env.except.IllegalStateException if this mock control is in replay state

setVoidCallableByVoid

function setVoidCallableByVoid (
Void) : Void
Records that the mock object will expect the last method call once and will
react by returning silently.
Throws:
org.as2lib.env.except.IllegalStateException if this mock control is in replay state

verify

function verify (
Void) : Void
Verifies that all expectations have been met that could not been verified
during execution.
Throws:
org.as2lib.env.except.IllegalStateException if this mock control is in record state
AssertionFailedError if an expectation has not been met