| java.lang.Object net.sf.surrogate.core.MockMethod
MockMethod | public class MockMethod implements SurrogateManager.MockExecutor(Code) | | 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"
See Also: SurrogateManager.addMockMethod(MockMethod) author: Per S Hustad |
Constructor Summary | |
public | MockMethod(Class theClass, String method, Class[] parameterTypes) Mocks a method. | public | MockMethod(Class theClass, String method) Mocks a method. | public | MockMethod(Class theClass, Class[] parameterTypes) Mocks a constructor.
This method is useful when it has been impossible to generate a mock
object for a class, e.g. |
MockMethod | public MockMethod(Class theClass, String method, Class[] parameterTypes) throws NoSuchMethodException(Code) | | Mocks a method. Useful if it has been impossible to generate a mock
object for a class, e.g. the class contains 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.
Parameters: theClass - the class containing the method to mock. Parameters: method - name of the method to mock. Must exist in the class. Parameters: parameterTypes - the formal parameters to the method. E.g. if the method isdeclared asint foo(int count,Context ccx,Integer obj) theformal parameters will be{Integer.TYPE,Context.class,Integer.class} throws: NoSuchMethodException - if the method cannot be found See Also: SurrogateManager.addMockMethod(MockMethod) |
MockMethod | public MockMethod(Class theClass, String method) throws NoSuchMethodException(Code) | | Mocks a method. This method is useful in the case where it has been
impossible to generate a mock object for a class, e.g. the class contains
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.
Parameters: theClass - the class containing the method to mock. Parameters: method - name of the method to mock. Must exist in the class andonly one method with that name must exist . If severaloverloaded methods exists with the same name, useMockMethod.MockMethod(Class,String,Class[]) throws: NoSuchMethodException - if the method cannot be found throws: IllegalArgumentException - if more than one method exists with the name. See Also: MockMethod.MockMethod(Class,String,Class[]) See Also: MockMethod.MockMethod(Class,Class[]) See Also: SurrogateManager.addMockMethod(MockMethod) |
MockMethod | public MockMethod(Class theClass, Class[] parameterTypes) throws NoSuchMethodException(Code) | | Mocks a constructor.
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 ....
Parameters: theClass - the class containing the constructor to mock. Parameters: parameterTypes - the formal parameters to the constructor. E.g. if theconstructor is declared asFoo(int count,Context ccx,Integer obj) theformal parameters will be{Integer.TYPE,Context.class,Integer.class} throws: NoSuchMethodException - if the constructor cannot be found See Also: SurrogateManager.addMockMethod(MockMethod) |
addExpectedParameters | public void addExpectedParameters(Object[] args)(Code) | | Sets up the next excpected arguments for the method. The actual arguments
are verified against the expected arguments when the actual method call
is performed. If they mismatch, an Exception will be thrown.
Parameters: args - array of Object values. NB: must correspond to theformal parameter types of the method or else a runtimeexception will be thrown. Primitive types should beencapsulated in their corresponding Object representation,e.g. int corresponds to Integer throws: IllegalArgumentException - if the number of aruments in args differs fromthe number of arguments in the underlying method orconstructor. |
addReturnValue | public void addReturnValue(Object o)(Code) | | Sets up the next return value of the mock method. If the method is called
more times than the calls to this method, the last specified return value
is used.
Parameters: o - the next return value. Primitive types are specified withtheir corresponding Object representation, e.g. an "int"corresponds to "Integer". throws: IllegalArgumentException - if the underlying AccessibleObject is a method returningvoid See Also: MockMethod.addThrowableReturnValue(Throwable) |
addThrowableReturnValue | public void addThrowableReturnValue(Throwable arg)(Code) | | Adds an errror to the list of return values.
Parameters: arg - the throwable to throw on the next call. The mock method willthrow this throwable even if the actual method does notdeclare it ... See Also: MockMethod.addReturnValue(Object) |
getAccessibleObject | AccessibleObject getAccessibleObject()(Code) | | Gets the java reflection method or constructor
the accessible object as specified in the constructor |
reset | public void reset()(Code) | | Resets the mock method counters, expectation values and stored actual
values.
|
setExpectedCalls | public void setExpectedCalls(int expectedCalls)(Code) | | Sets the number of expected calls to this method. The
MockMethod.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.
Parameters: expectedCalls - the number of expected calls. See Also: MockMethod.verify() |
toString | public String toString()(Code) | | Gets a textual representation of this MockMethod
the underlying method or constructor's toString |
|
|