net.sf.surrogate.core
Class MockMethod

java.lang.Object
  |
  +--net.sf.surrogate.core.MockMethod
All Implemented Interfaces:
SurrogateManager.MockExecutor

public class MockMethod
extends java.lang.Object
implements SurrogateManager.MockExecutor

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"

Author:
Per S Hustad
See Also:
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
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MockMethod

public MockMethod(java.lang.Class theClass,
                  java.lang.String method,
                  java.lang.Class[] parameterTypes)
           throws java.lang.NoSuchMethodException
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.
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}
Throws:
java.lang.NoSuchMethodException - if the method cannot be found
See Also:
SurrogateManager.addMockMethod(MockMethod)

MockMethod

public MockMethod(java.lang.Class theClass,
                  java.lang.String method)
           throws java.lang.NoSuchMethodException
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 SurrogateCallsfor pointcut definition details.

Parameters:
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[])
Throws:
java.lang.NoSuchMethodException - if the method cannot be found
java.lang.IllegalArgumentException - if more than one method exists with the name.
See Also:
MockMethod(Class, String, Class[]), MockMethod(Class, Class[]), SurrogateManager.addMockMethod(MockMethod)

MockMethod

public MockMethod(java.lang.Class theClass,
                  java.lang.Class[] parameterTypes)
           throws java.lang.NoSuchMethodException
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 SurrogateCallsfor 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.
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}
Throws:
java.lang.NoSuchMethodException - if the constructor cannot be found
See Also:
SurrogateManager.addMockMethod(MockMethod)
Method Detail

reset

public void reset()
Resets the mock method counters, expectation values and stored actual values.

setExpectedCalls

public void setExpectedCalls(int expectedCalls)
Sets the number of expected calls to this method. The 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:
verify()

addReturnValue

public void addReturnValue(java.lang.Object o)
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 with their corresponding Object representation, e.g. an "int" corresponds to "Integer".
Throws:
java.lang.IllegalArgumentException - if the underlying AccessibleObject is a method returning void
See Also:
addThrowableReturnValue(Throwable)

addThrowableReturnValue

public void addThrowableReturnValue(java.lang.Throwable arg)
Adds an errror to the list of return values.
Parameters:
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 ...
See Also:
addReturnValue(Object)

addExpectedParameters

public void addExpectedParameters(java.lang.Object[] args)
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 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 Integer
Throws:
java.lang.IllegalArgumentException - if the number of aruments in args differs from the number of arguments in the underlying method or constructor.

execute

public java.lang.Object execute(java.lang.Object[] args)
                         throws java.lang.Throwable
Performs the actual method call. This method should normally not be called from test harnesses. It is called from the Surrogate core framework
Specified by:
execute in interface SurrogateManager.MockExecutor
Parameters:
args - the aruments to the method. Will be compared to the expected values if such values have been specified.
Returns:
the return value given in addReturnValue(Object)unless the next return value has been set with the addThrowableReturnValue(Throwable)call. In this case, an Exception is thrown
Throws:
java.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 signature

getAccessibleObject

java.lang.reflect.AccessibleObject getAccessibleObject()
Gets the java reflection method or constructor
Returns:
the accessible object as specified in the constructor

verify

public void verify()
Verifies that the method has been called as many times as specified in setExpectedCalls(int)
See Also:
setExpectedCalls(int)

toString

public java.lang.String toString()
Gets a textual representation of this MockMethod
Overrides:
toString in class java.lang.Object
Returns:
the underlying method or constructor's toString


Copyright © 2005 . All Rights Reserved.