com.hammingweight.hammock
Class MethodHandler

java.lang.Object
  extended by com.hammingweight.hammock.MethodHandler
All Implemented Interfaces:
IClassDefinitions, IHammockExceptionErrors

public class MethodHandler
extends java.lang.Object
implements IHammockExceptionErrors, IClassDefinitions

The MethodHandler class creates a handler for processing a method invocation. The handler can be configured to respond more than once to a method invocation but will always respond identically. This class is used to set expectations for mock objects.

Since:
Hammock 1.0
Author:
C.A. Meijer

Field Summary
 
Fields inherited from interface com.hammingweight.hammock.IHammockExceptionErrors
ARGUMENT_MATCHER_ALREADY_SET, CANT_SET_VOID_RETURN_VALUE, CLASS_IS_NOT_THROWABLE, DOES_NOT_SUBCLASS_MOCK_OBJECT, HANDLER_NOT_SET, INCORRECT_MOCK_OBJECT, INCORRECT_RETURN_CLASS, INCORRECT_THROWABLE_CLASS, METHOD_CANNOT_BE_EVALUATED, METHOD_INVOKED_OUT_OF_SEQUENCE, METHOD_INVOKED_TOO_MANY_TIMES, METHOD_INVOKED_UNEXPECTED_NUMBER_OF_TIMES, NO_RETURN_VALUE, NO_RETURN_VALUE_SET, NUM_INVOCATIONS_ALREADY_SET, RETURN_VALUE_ALREADY_SET, WRONG_ARGUMENT_CLASS, WRONG_NUMBER_OF_ARGS
 
Fields inherited from interface com.hammingweight.hammock.IClassDefinitions
BOOLEAN_CLASS, BYTE_CLASS, CHARACTER_CLASS, DOUBLE_CLASS, FLOAT_CLASS, IMOCKOBJECT_CLASS, INTEGER_CLASS, LONG_CLASS, OBJECT_CLASS, SHORT_CLASS, THROWABLE_CLASS
 
Constructor Summary
MethodHandler(MethodInvocation mi)
          Constructor.
MethodHandler(MockMethod mockMethod)
          Constructor.
MethodHandler(MockMethod mockMethod, IMockObject mockObject)
          Constructor.
MethodHandler(MockMethod mockMethod, IMockObject mockObject, java.lang.Object[] args)
          Constructor.
MethodHandler(MockMethod mockMethod, java.lang.Object[] args)
          Constructor.
 
Method Summary
 MethodHandler ignoreArgument(int argIndex)
          A method to set an argument that should be ignored when determining whether this handler responds to a particular method invocation.
 void invoke(MethodInvocation mi)
          A method that invokes the handler.
 MethodHandler setArgumentMatcher(int argIndex, IArgumentMatcher argMatcher)
          This method adds an argument matcher to be added to a method handler so that two arguments, object1 and object2 can be determined to be equal even if object1.equals(object2) returns false.
 MethodHandler setDelay(long delayInMs)
          A method to configure a delay before this handler responds to a method invocation.
 MethodHandler setInvocationCount(int expectedNumInvocations)
          Sets the number of times that we expect a method to be invoked.
 MethodHandler setInvocationCount(int minNumber, int maxNumber)
          Sets upper and lower limits for the number of times that we expect a method to be invoked.
 MethodHandler setReturnValue(java.lang.Object retVal)
          Sets the value to be returned if a method is invoked.
 MethodHandler setThrowable(java.lang.Throwable t)
          Sets an exception to be thrown when the method is invoked.
 void verify()
          A method that verifies that the handler was invoked as expected.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MethodHandler

public MethodHandler(MockMethod mockMethod)
Constructor.

Parameters:
mockMethod - The method that this handler will respond to.

MethodHandler

public MethodHandler(MockMethod mockMethod,
                     java.lang.Object[] args)
Constructor.

Parameters:
mockMethod - The method that this handler responds to.
args - The expected arguments.

MethodHandler

public MethodHandler(MethodInvocation mi)
Constructor.

Parameters:
mi - The method invocation (specifying a method, a mock object and arguments) that this handler will respond to.
Since:
Hammock 1.6.

MethodHandler

public MethodHandler(MockMethod mockMethod,
                     IMockObject mockObject)
Constructor.

Parameters:
mockMethod - The method that this handler responds to.
mockObject - The object on which the method is invoked.
Since:
Hammock 2.0

MethodHandler

public MethodHandler(MockMethod mockMethod,
                     IMockObject mockObject,
                     java.lang.Object[] args)
Constructor.

Parameters:
mockMethod - The method that this handler responds to.
mockObject - The object on which the method is invoked.
args - The expected arguments.
Method Detail

setInvocationCount

public MethodHandler setInvocationCount(int expectedNumInvocations)
Sets the number of times that we expect a method to be invoked.

Parameters:
expectedNumInvocations - The expected number of invocations.
Returns:
this.
Since:
Hammock 1.6

setInvocationCount

public MethodHandler setInvocationCount(int minNumber,
                                        int maxNumber)
Sets upper and lower limits for the number of times that we expect a method to be invoked.

Parameters:
minNumber - A lower expected limit.
maxNumber - An upper expected limit.
Returns:
this.
Since:
Hammock 1.6

setReturnValue

public MethodHandler setReturnValue(java.lang.Object retVal)
Sets the value to be returned if a method is invoked.

Parameters:
retVal - The value to be returned when the method is invoked. Primitive values must be wrapped by the appropriate wrapper class (e.g. an int must be wrapped by an Integer .
Returns:
this.

setThrowable

public MethodHandler setThrowable(java.lang.Throwable t)
Sets an exception to be thrown when the method is invoked.

Parameters:
t - The exception (or error) to throw,
Returns:
this.

verify

public void verify()
A method that verifies that the handler was invoked as expected.


invoke

public void invoke(MethodInvocation mi)
A method that invokes the handler. The handler sets the return value or exception to throw.

Parameters:
mi - The invoked method.

setDelay

public MethodHandler setDelay(long delayInMs)
A method to configure a delay before this handler responds to a method invocation.

Parameters:
delayInMs - The delay in milliseconds.
Returns:
this.

ignoreArgument

public MethodHandler ignoreArgument(int argIndex)
A method to set an argument that should be ignored when determining whether this handler responds to a particular method invocation.

Parameters:
argIndex - The argument number (indexed from zero) that can be ignored.
Returns:
this.

setArgumentMatcher

public MethodHandler setArgumentMatcher(int argIndex,
                                        IArgumentMatcher argMatcher)
This method adds an argument matcher to be added to a method handler so that two arguments, object1 and object2 can be determined to be equal even if object1.equals(object2) returns false.

Parameters:
argIndex - The index of the argument in the method's signature.
argMatcher - A class that can determine whether two objects are equal.
Returns:
this
Since:
Hammock 1.4.