Class org.as2lib.test.mock.MockControl

org.as2lib.core.BasicClass
   +--org.as2lib.test.mock.MockControl

Description

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.

Method Index

new MockControl()

areToStringInvocationsHandled(), getDefaultArgumentsMatcher(), getMock(), getMockProxyFactory(), getRecordStateFactory(), getReplayStateFactory(), getTypeArgumentsMatcher(), replay(), reset(), setArgumentsMatcher(), setDefaultReturnValue(), setDefaultThrowable(), setDefaultVoidCallable(), setHandleToStringInvocations(), setMockProxyFactory(), setRecordStateFactory(), setReplayStateFactory(), setReturnValue(), setReturnValueByValue(), setReturnValueByValueAndMinimumAndMaximumQuantity(), setReturnValueByValueAndQuantity(), setThrowable(), setThrowableByThrowable(), setThrowableByThrowableAndMinimumAndMaximumQuantity(), setThrowableByThrowableAndQuantity(), setVoidCallable(), setVoidCallableByMinimumAndMaximumQuantity(), setVoidCallableByQuantity(), setVoidCallableByVoid(), verify()

Inherited from BasicClass

toString()

Constructor Detail

MockControl

public function MockControl()

Method Detail

getDefaultArgumentsMatcher

static public function getDefaultArgumentsMatcher(Void):DefaultArgumentsMatcher

Returns a new default arguments matcher.

Return

a new default arguments matcher

getTypeArgumentsMatcher

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.

Return

a type arguments matcher

setHandleToStringInvocations

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.

Parameters

handleToStringInvocationsdetermines whether to handle toStirng method invocations

areToStringInvocationsHandled

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.

Return

true if toString invocations are handled else false

getMockProxyFactory

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.

Return

the currently used proxy factory

setMockProxyFactory

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.

Parameters

proxyFactoryfactory to obtain mock proxies / mocks

getRecordStateFactory

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.

Return

the currently used record state factory

setRecordStateFactory

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.

Parameters

recordStateFactorythe new record state factory

getReplayStateFactory

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.

Return

the currently used replay state factory

setReplayStateFactory

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.

Parameters

replayStateFactorythe new replay state factory

getMock

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.

Return

the mock object

replay

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.

reset

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.

setArgumentsMatcher

public function setArgumentsMatcher(argumentsMatcher:ArgumentsMatcher):Void

Sets the arguments matcher that will be used for the last method specified by a method call.

Parameters

argumentsMatcherthe arguments matcher to use for the specific method

Throws

IllegalStateExceptionif this mock control is in replay state

setDefaultReturnValue

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.

Parameters

valuethe return value to return

Throws

IllegalStateExceptionif this mock control is in replay state

setDefaultThrowable

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.

Parameters

throwablethe throwable to throw

Throws

IllegalStateExceptionif this mock control is in replay state

setDefaultVoidCallable

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.

Throws

IllegalStateExceptionif this mock control is in replay state

setReturnValue

public function setReturnValue():Void

setReturnValueByValue

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.

Parameters

valuethe return value to return

Throws

IllegalStateExceptionif this mock control is in replay state

setReturnValueByValueAndQuantity

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.

Parameters

valuethe return value to return
quantitythe number of times the method is allowed to be invoked

Throws

IllegalStateExceptionif this mock control is in replay state

setReturnValueByValueAndMinimumAndMaximumQuantity

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.

Parameters

valuethe return value to return
minimumQuantitythe minimum number of times the method must be called
maximumQuantitythe maximum number of times the method can be called

Throws

IllegalStateExceptionif this mock control is in replay state

setThrowable

public function setThrowable():Void

setThrowableByThrowable

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.

Parameters

throwablethe throwable to throw

Throws

IllegalStateExceptionif this mock control is in replay state

setThrowableByThrowableAndQuantity

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.

Parameters

throwablethe throwable to throw
quantitythe number of times the method is allowed to be invoked

Throws

IllegalStateExceptionif this mock control is in replay state

setThrowableByThrowableAndMinimumAndMaximumQuantity

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.

Parameters

throwablethe throwable to throw
minimumQuantitythe minimum number of times the method must be called
maximumQuantitythe maximum number of times the method can be called

Throws

IllegalStateExceptionif this mock control is in replay state

setVoidCallable

public function setVoidCallable():Void

setVoidCallableByVoid

public function setVoidCallableByVoid(Void):Void

Records that the mock object will expect the last method call once and will react by returning silently.

Throws

IllegalStateExceptionif this mock control is in replay state

setVoidCallableByQuantity

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.

Parameters

quantitythe number of times the method is allowed to be invoked

Throws

IllegalStateExceptionif this mock control is in replay state

setVoidCallableByMinimumAndMaximumQuantity

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.

Parameters

minimumQuantitythe minimum number of times the method must be called
maximumQuantitythe maximum number of times the method can be called

Throws

IllegalStateExceptionif this mock control is in replay state

verify

public function verify(Void):Void

Verifies that all expectations have been met that could not been verified during execution.

Throws

IllegalStateExceptionif this mock control is in record state
AssertionFailedErrorif an expectation has not been met