Package org.easymock.internal
Class MocksControl
- java.lang.Object
-
- org.easymock.internal.MocksControl
-
- All Implemented Interfaces:
java.io.Serializable,IExpectationSetters<java.lang.Object>,IMocksControl
public class MocksControl extends java.lang.Object implements IMocksControl, IExpectationSetters<java.lang.Object>, java.io.Serializable
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static RangeAT_LEAST_ONCEOne or more calls.private IMocksBehaviorbehaviorprivate static IProxyFactoryclassProxyFactorylazily created; the proxy factory for classesprivate static IProxyFactoryinterfaceProxyFactorystatic RangeONCEExactly one call.private static longserialVersionUIDprivate IMocksControlStatestateprivate MockTypetypestatic RangeZERO_OR_MOREZero or more calls.
-
Constructor Summary
Constructors Constructor Description MocksControl(MockType type)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description IExpectationSetters<java.lang.Object>andAnswer(IAnswer<?> answer)Sets an object that will be used to calculate the answer for the expected invocation (either return a value, or throw an exception).IExpectationSetters<java.lang.Object>andDelegateTo(java.lang.Object answer)Sets an object implementing the same interface as the mock.IExpectationSetters<java.lang.Object>andReturn(java.lang.Object value)Sets a return value that will be returned for the expected invocation.voidandStubAnswer(IAnswer<?> answer)Sets a stub object that will be used to calculate the answer for the expected invocation (either return a value, or throw an exception).voidandStubDelegateTo(java.lang.Object delegateTo)Sets a stub object implementing the same interface as the mock.voidandStubReturn(java.lang.Object value)Sets a stub return value that will be returned for the expected invocation.voidandStubThrow(java.lang.Throwable throwable)Sets a stub throwable that will be thrown for the expected invocation.IExpectationSetters<java.lang.Object>andThrow(java.lang.Throwable throwable)Sets a throwable that will be thrown for the expected invocation.IExpectationSetters<java.lang.Object>andVoid()Records a call but returns nothing.IExpectationSetters<java.lang.Object>anyTimes()Expect the last invocation any times.voidasStub()Sets stub behavior for the expected invocation (this is needed for void methods).IExpectationSetters<java.lang.Object>atLeastOnce()Expect the last invocation at least once.voidcheckIsUsedInOneThread(boolean shouldBeUsedInOneThread)Check that the mock is called from only one threadvoidcheckOrder(boolean value)Switches order checking on and off.<T,R>
RcreateMock(java.lang.Class<T> toMock)Same asIMocksControl.mock(Class)but using the old naming.<T,R>
RcreateMock(java.lang.String name, java.lang.Class<T> toMock)Same asIMocksControl.mock(String, Class)but using the old naming.<T,R>
RcreateMock(java.lang.String name, java.lang.Class<T> toMock, ConstructorArgs constructorArgs, java.lang.reflect.Method... mockedMethods)Same asIMocksControl.mock(String, Class, ConstructorArgs, Method...)but using the old namingprivate static IProxyFactorygetClassProxyFactory()static MocksControlgetControl(java.lang.Object mock)static java.lang.reflect.InvocationHandlergetInvocationHandler(java.lang.Object mock)static <T,R extends T>
java.lang.Class<R>getMockedClass(T proxy)Return the class of interface (depending on the mock type) that was mockedstatic IProxyFactorygetProxyFactory(java.lang.Object o)IMocksControlStategetState()MockTypegetType()voidmakeThreadSafe(boolean threadSafe)Makes the mock thread safe.IExpectationSetters<java.lang.Object>once()Expect the last invocation once.voidreplay()Switches the control from record mode to replay mode.voidreset()Removes all expectations for the mock objects of this control.voidresetToDefault()Removes all expectations for the mock objects of this control and turn them to default mocks.voidresetToNice()Removes all expectations for the mock objects of this control and turn them to nice mocks.voidresetToStrict()Removes all expectations for the mock objects of this control and turn them to strict mocks.IExpectationSetters<java.lang.Object>times(int times)Expect the last invocationcounttimes.IExpectationSetters<java.lang.Object>times(int min, int max)Expect the last invocation betweenminandmaxtimes.voidverify()Verifies that all expectations were met and that no unexpected call was performed.voidverifyRecording()Verifies that all expectations were met.voidverifyUnexpectedCalls()Verifies that no unexpected call was performed.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.easymock.IMocksControl
mock, mock, mock
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
ONCE
public static final Range ONCE
Exactly one call.
-
AT_LEAST_ONCE
public static final Range AT_LEAST_ONCE
One or more calls.
-
ZERO_OR_MORE
public static final Range ZERO_OR_MORE
Zero or more calls.
-
classProxyFactory
private static volatile IProxyFactory classProxyFactory
lazily created; the proxy factory for classes
-
interfaceProxyFactory
private static final IProxyFactory interfaceProxyFactory
-
state
private IMocksControlState state
-
behavior
private IMocksBehavior behavior
-
type
private MockType type
-
-
Constructor Detail
-
MocksControl
public MocksControl(MockType type)
-
-
Method Detail
-
getType
public MockType getType()
-
getState
public IMocksControlState getState()
-
createMock
public <T,R> R createMock(java.lang.Class<T> toMock)
Description copied from interface:IMocksControlSame asIMocksControl.mock(Class)but using the old naming.- Specified by:
createMockin interfaceIMocksControl- Type Parameters:
T- the interface or class that the mock object should implement/extend.R- the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the caller, we return a different type- Parameters:
toMock- the interface or class that the mock object should implement/extend.- Returns:
- the mock object.
-
createMock
public <T,R> R createMock(java.lang.String name, java.lang.Class<T> toMock)Description copied from interface:IMocksControlSame asIMocksControl.mock(String, Class)but using the old naming.- Specified by:
createMockin interfaceIMocksControl- Type Parameters:
T- the interface or class that the mock object should implement/extend.R- the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the caller, we return a different type- Parameters:
name- the name of the mock object.toMock- the interface or class that the mock object should implement/extend.- Returns:
- the mock object.
-
createMock
public <T,R> R createMock(java.lang.String name, java.lang.Class<T> toMock, ConstructorArgs constructorArgs, java.lang.reflect.Method... mockedMethods)Description copied from interface:IMocksControlSame asIMocksControl.mock(String, Class, ConstructorArgs, Method...)but using the old naming- Specified by:
createMockin interfaceIMocksControl- Type Parameters:
T- the class that the mock object should extend.R- the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the caller, we return a different type- Parameters:
name- the name of the mock object.toMock- the class that the mock object should extend.constructorArgs- constructor and parameters used to instantiate the mock. If null, no constructor will be calledmockedMethods- methods that will be mocked, other methods will behave normally. If empty, all methods will be mocked- Returns:
- the mock object.
-
getProxyFactory
public static IProxyFactory getProxyFactory(java.lang.Object o)
-
getClassProxyFactory
private static IProxyFactory getClassProxyFactory()
-
getControl
public static MocksControl getControl(java.lang.Object mock)
-
getInvocationHandler
public static java.lang.reflect.InvocationHandler getInvocationHandler(java.lang.Object mock)
-
getMockedClass
public static <T,R extends T> java.lang.Class<R> getMockedClass(T proxy)
Return the class of interface (depending on the mock type) that was mocked- Type Parameters:
T- Mocked classR- Mock class- Parameters:
proxy- Mock object- Returns:
- the mocked class or interface
-
reset
public void reset()
Description copied from interface:IMocksControlRemoves all expectations for the mock objects of this control.- Specified by:
resetin interfaceIMocksControl
-
resetToNice
public void resetToNice()
Description copied from interface:IMocksControlRemoves all expectations for the mock objects of this control and turn them to nice mocks.- Specified by:
resetToNicein interfaceIMocksControl
-
resetToDefault
public void resetToDefault()
Description copied from interface:IMocksControlRemoves all expectations for the mock objects of this control and turn them to default mocks.- Specified by:
resetToDefaultin interfaceIMocksControl
-
resetToStrict
public void resetToStrict()
Description copied from interface:IMocksControlRemoves all expectations for the mock objects of this control and turn them to strict mocks.- Specified by:
resetToStrictin interfaceIMocksControl
-
replay
public void replay()
Description copied from interface:IMocksControlSwitches the control from record mode to replay mode.- Specified by:
replayin interfaceIMocksControl
-
verifyRecording
public void verifyRecording()
Description copied from interface:IMocksControlVerifies that all expectations were met.- Specified by:
verifyRecordingin interfaceIMocksControl
-
verifyUnexpectedCalls
public void verifyUnexpectedCalls()
Description copied from interface:IMocksControlVerifies that no unexpected call was performed.- Specified by:
verifyUnexpectedCallsin interfaceIMocksControl
-
verify
public void verify()
Description copied from interface:IMocksControlVerifies that all expectations were met and that no unexpected call was performed. It has the same effect as callingIMocksControl.verifyRecording()followed byIMocksControl.verifyUnexpectedCalls().- Specified by:
verifyin interfaceIMocksControl
-
checkOrder
public void checkOrder(boolean value)
Description copied from interface:IMocksControlSwitches order checking on and off.- Specified by:
checkOrderin interfaceIMocksControl- Parameters:
value-trueswitches order checking on,falseswitches it off.
-
makeThreadSafe
public void makeThreadSafe(boolean threadSafe)
Description copied from interface:IMocksControlMakes the mock thread safe.- Specified by:
makeThreadSafein interfaceIMocksControl- Parameters:
threadSafe- If the mock should be thread safe or not
-
checkIsUsedInOneThread
public void checkIsUsedInOneThread(boolean shouldBeUsedInOneThread)
Description copied from interface:IMocksControlCheck that the mock is called from only one thread- Specified by:
checkIsUsedInOneThreadin interfaceIMocksControl- Parameters:
shouldBeUsedInOneThread- If it should be used in one thread only or not
-
andReturn
public IExpectationSetters<java.lang.Object> andReturn(java.lang.Object value)
Description copied from interface:IExpectationSettersSets a return value that will be returned for the expected invocation.- Specified by:
andReturnin interfaceIExpectationSetters<java.lang.Object>- Parameters:
value- the value to return.- Returns:
- this object to allow method call chaining.
-
andThrow
public IExpectationSetters<java.lang.Object> andThrow(java.lang.Throwable throwable)
Description copied from interface:IExpectationSettersSets a throwable that will be thrown for the expected invocation.- Specified by:
andThrowin interfaceIExpectationSetters<java.lang.Object>- Parameters:
throwable- the throwable to throw.- Returns:
- this object to allow method call chaining.
-
andAnswer
public IExpectationSetters<java.lang.Object> andAnswer(IAnswer<?> answer)
Description copied from interface:IExpectationSettersSets an object that will be used to calculate the answer for the expected invocation (either return a value, or throw an exception).- Specified by:
andAnswerin interfaceIExpectationSetters<java.lang.Object>- Parameters:
answer- the object used to answer the invocation.- Returns:
- this object to allow method call chaining.
-
andDelegateTo
public IExpectationSetters<java.lang.Object> andDelegateTo(java.lang.Object answer)
Description copied from interface:IExpectationSettersSets an object implementing the same interface as the mock. The expected method call will be delegated to it with the actual arguments. The answer returned by this call will then be the answer returned by the mock (either return a value, or throw an exception).- Specified by:
andDelegateToin interfaceIExpectationSetters<java.lang.Object>- Parameters:
answer- the object the call is delegated to.- Returns:
- the value returned by the delegated call.
-
andVoid
public IExpectationSetters<java.lang.Object> andVoid()
Description copied from interface:IExpectationSettersRecords a call but returns nothing. Used to chain calls on void methodsexpectLastCall().andThrow(e).andVoid()- Specified by:
andVoidin interfaceIExpectationSetters<java.lang.Object>- Returns:
- this object to allow method call chaining.
-
andStubReturn
public void andStubReturn(java.lang.Object value)
Description copied from interface:IExpectationSettersSets a stub return value that will be returned for the expected invocation.- Specified by:
andStubReturnin interfaceIExpectationSetters<java.lang.Object>- Parameters:
value- the value to return.
-
andStubThrow
public void andStubThrow(java.lang.Throwable throwable)
Description copied from interface:IExpectationSettersSets a stub throwable that will be thrown for the expected invocation.- Specified by:
andStubThrowin interfaceIExpectationSetters<java.lang.Object>- Parameters:
throwable- the throwable to throw.
-
andStubAnswer
public void andStubAnswer(IAnswer<?> answer)
Description copied from interface:IExpectationSettersSets a stub object that will be used to calculate the answer for the expected invocation (either return a value, or throw an exception).- Specified by:
andStubAnswerin interfaceIExpectationSetters<java.lang.Object>- Parameters:
answer- the object used to answer the invocation.
-
andStubDelegateTo
public void andStubDelegateTo(java.lang.Object delegateTo)
Description copied from interface:IExpectationSettersSets a stub object implementing the same interface as the mock. The expected method call will be delegated to it with the actual arguments. The answer returned by this call will then be the answer returned by the mock (either return a value, or throw an exception).- Specified by:
andStubDelegateToin interfaceIExpectationSetters<java.lang.Object>- Parameters:
delegateTo- the object the call is delegated to.
-
asStub
public void asStub()
Description copied from interface:IExpectationSettersSets stub behavior for the expected invocation (this is needed for void methods).- Specified by:
asStubin interfaceIExpectationSetters<java.lang.Object>
-
times
public IExpectationSetters<java.lang.Object> times(int times)
Description copied from interface:IExpectationSettersExpect the last invocationcounttimes.- Specified by:
timesin interfaceIExpectationSetters<java.lang.Object>- Parameters:
times- the number of invocations expected- Returns:
- this object to allow method call chaining.
-
times
public IExpectationSetters<java.lang.Object> times(int min, int max)
Description copied from interface:IExpectationSettersExpect the last invocation betweenminandmaxtimes.- Specified by:
timesin interfaceIExpectationSetters<java.lang.Object>- Parameters:
min- the minimum number of invocations expected.max- the maximum number of invocations expected.- Returns:
- this object to allow method call chaining.
-
once
public IExpectationSetters<java.lang.Object> once()
Description copied from interface:IExpectationSettersExpect the last invocation once. This is default in EasyMock.- Specified by:
oncein interfaceIExpectationSetters<java.lang.Object>- Returns:
- this object to allow method call chaining.
-
atLeastOnce
public IExpectationSetters<java.lang.Object> atLeastOnce()
Description copied from interface:IExpectationSettersExpect the last invocation at least once.- Specified by:
atLeastOncein interfaceIExpectationSetters<java.lang.Object>- Returns:
- this object to allow method call chaining.
-
anyTimes
public IExpectationSetters<java.lang.Object> anyTimes()
Description copied from interface:IExpectationSettersExpect the last invocation any times.- Specified by:
anyTimesin interfaceIExpectationSetters<java.lang.Object>- Returns:
- this object to allow method call chaining.
-
-