|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--net.sf.surrogate.core.MockMethod
General mock class to mock a specific method. Useful to mock "final" methods where it is impossible to override the actual class with a Mock implementation.
Example:
SurrogateManager manager = SurrogateManager.getInstance(); manager.reset(); MockMethod mockTime = new MockMethod(java.lang.System.class,"currentTimeMillis"); manager.addMockMethod(mockTime); mockTime.addReturnValue(new Long(2000)); mockTime.addReturnValue(new Long(4000)); mockTime.setExpectedCalls(4); ... ... mockTime.verify();
Registers a mock method for the java.lang.System.currentTimeMillis
method and specifies that the method should be called exactly four times,
or else an error is generated. The first call returns "2000" and
the next three calls return "4000"
SurrogateManager.addMockMethod(MockMethod)
Constructor Summary | |
MockMethod(java.lang.Class theClass,
java.lang.Class[] parameterTypes)
Mocks a constructor. |
|
MockMethod(java.lang.Class theClass,
java.lang.String method)
Mocks a method. |
|
MockMethod(java.lang.Class theClass,
java.lang.String method,
java.lang.Class[] parameterTypes)
Mocks a method. |
Method Summary | |
void |
addExpectedParameters(java.lang.Object[] args)
Sets up the next excpected arguments for the method. |
void |
addReturnValue(java.lang.Object o)
Sets up the next return value of the mock method. |
void |
addThrowableReturnValue(java.lang.Throwable arg)
Adds an errror to the list of return values. |
java.lang.Object |
execute(java.lang.Object[] args)
Performs the actual method call. |
(package private) java.lang.reflect.AccessibleObject |
getAccessibleObject()
Gets the java reflection method or constructor |
void |
reset()
Resets the mock method counters, expectation values and stored actual values. |
void |
setExpectedCalls(int expectedCalls)
Sets the number of expected calls to this method. |
java.lang.String |
toString()
Gets a textual representation of this MockMethod |
void |
verify()
Verifies that the method has been called as many times as specified in setExpectedCalls(int) |
Methods inherited from class java.lang.Object |
|
Constructor Detail |
public MockMethod(java.lang.Class theClass, java.lang.String method, java.lang.Class[] parameterTypes) throws java.lang.NoSuchMethodException
final
methods
or we want to mock a static
method.
Note that for methods to be substituted with their mock implementation,
there must have been defined a pointcut
intercepting the
method call or method execution. Otherwise, the mock method will never be
called, even if it has been registered.
See net.sf.surrogate.core.SurrogateCalls
for
pointcut definition details.
theClass
- the class containing the method to mock.method
- name of the method to mock. Must exist in the class.parameterTypes
- the formal parameters to the method. E.g. if the method is
declared as
int foo(int count,Context ccx,Integer obj)
the
formal parameters will be
{Integer.TYPE,Context.class,Integer.class}
java.lang.NoSuchMethodException
- if the method cannot be foundSurrogateManager.addMockMethod(MockMethod)
public MockMethod(java.lang.Class theClass, java.lang.String method) throws java.lang.NoSuchMethodException
final
methods or we want to mock a static
method.
Note that for methods to be substituted with their mock implementation,
there must have been defined a pointcut
intercepting the
method call or method execution. Otherwise, the mock method will never be
called, even if it has been registered. See SurrogateCalls
for
pointcut definition details.
theClass
- the class containing the method to mock.method
- name of the method to mock. Must exist in the class and
only one method with that name must exist . If several
overloaded methods exists with the same name, use
MockMethod(Class,String,Class[])
java.lang.NoSuchMethodException
- if the method cannot be foundjava.lang.IllegalArgumentException
- if more than one method exists with the name.MockMethod(Class, String, Class[])
,
MockMethod(Class, Class[])
,
SurrogateManager.addMockMethod(MockMethod)
public MockMethod(java.lang.Class theClass, java.lang.Class[] parameterTypes) throws java.lang.NoSuchMethodException
This method is useful when it has been impossible to generate a mock object for a class, e.g. the class is final or the "real" constructor makes some unwanted procssing.
Note that for constructors to be substituted with their mock
implementation, there must have been defined a pointcut
intercepting the constructor call or execution. Otherwise, the mock
method will never be called, even if it has been registered. See
SurrogateCalls
for pointcut definition details.
Note, that for mock constructors, the corresponding pointcut
should be a call type pointcut if the corresponding
MockMethod
is to return some values. If the
pointcut
is "execution", the return values from the
MockMethod will disappear somewhere in the VM ....
theClass
- the class containing the constructor to mock.parameterTypes
- the formal parameters to the constructor. E.g. if the
constructor is declared as
Foo(int count,Context ccx,Integer obj)
the
formal parameters will be
{Integer.TYPE,Context.class,Integer.class}
java.lang.NoSuchMethodException
- if the constructor cannot be foundSurrogateManager.addMockMethod(MockMethod)
Method Detail |
public void reset()
public void setExpectedCalls(int expectedCalls)
verify()
method verifies the calls. Note that if you don't call
this method after a reset or creation, the verify method will not
check the actual calls.expectedCalls
- the number of expected calls.verify()
public void addReturnValue(java.lang.Object o)
o
- the next return value. Primitive types are specified with
their corresponding Object representation, e.g. an "int"
corresponds to "Integer".java.lang.IllegalArgumentException
- if the underlying AccessibleObject is a method returning
void
addThrowableReturnValue(Throwable)
public void addThrowableReturnValue(java.lang.Throwable arg)
arg
- the throwable to throw on the next call. The mock method will
throw this throwable even if the actual method does not
declare it ...addReturnValue(Object)
public void addExpectedParameters(java.lang.Object[] args)
args
- array of Object values. NB: must correspond to the
formal parameter types of the method or else a runtime
exception will be thrown. Primitive types should be
encapsulated in their corresponding Object representation,
e.g. int corresponds to Integerjava.lang.IllegalArgumentException
- if the number of aruments in args
differs from
the number of arguments in the underlying method or
constructor.public java.lang.Object execute(java.lang.Object[] args) throws java.lang.Throwable
execute
in interface SurrogateManager.MockExecutor
args
- the aruments to the method. Will be compared to the expected
values if such values have been specified.addReturnValue(Object)
unless
the next return value has been set with the
addThrowableReturnValue(Throwable)
call. In this case,
an Exception is thrownjava.lang.Throwable
- if an exception has been set up in
addThrowableReturnValue(Throwable)
or if the number of parameters in
args
does not match the number of parameters
in the method signaturejava.lang.reflect.AccessibleObject getAccessibleObject()
public void verify()
setExpectedCalls(int)
setExpectedCalls(int)
public java.lang.String toString()
toString
in class java.lang.Object
toString
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |