com.hammingweight.hammock
Class Hamspy

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

public class Hamspy
extends ATestDoubleHandler

The Hamspy class is an IInvocationHandler that causes a test double associated with the handler to behave like a spy object. The Hamspy class can also behave like a mock object that responds to methods set via the setExpectation() method.

Since:
Hammock 1.6
Author:
C.A. Meijer

Field Summary
 
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
 
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
 
Constructor Summary
Hamspy()
          Constructor; equivalent to Hamspy(false).
Hamspy(boolean stubConcreteMethods)
          A constructor that determines whether this spy handler should respond to invocations of non-abstract methods.
 
Method Summary
 InvocationVerifier getExpectation(MockMethod method)
          A method to get a verifier for checking that a method was invoked as expected.
 InvocationVerifier getExpectation(MockMethod method, IMockObject mockObject)
          A method to get a verifier for checking that a method was invoked as expected.
 InvocationVerifier getExpectation(MockMethod method, IMockObject mockObject, java.lang.Object[] methodArgs)
          A method to get a verifier for checking that a method was invoked as expected.
 InvocationVerifier getExpectation(MockMethod method, java.lang.Object[] methodArgs)
          A method to get a verifier for checking that a method was invoked as expected.
 MethodInvocation[] getInvocations()
          A method to get all the invocations that were spied on.
 MethodInvocation[] getInvocations(MockMethod method)
          A method to get all invocations of a particular method.
 MethodInvocation[] getInvocations(MockMethod method, IMockObject mockObject)
          A method to get the invocations of a particular method on a specific (mock) object.
 void invoke(MethodInvocation mi)
          Processes a method invocation on a spy object.
 
Methods inherited from class com.hammingweight.hammock.ATestDoubleHandler
getExpectation, getNumberOfExpectations, setExpectation, setExpectation, setExpectation, setExpectation, setStubExpectation, setStubExpectation, setStubExpectation, setStubExpectation
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Hamspy

public Hamspy()
Constructor; equivalent to Hamspy(false).


Hamspy

public Hamspy(boolean stubConcreteMethods)
A constructor that determines whether this spy handler should respond to invocations of non-abstract methods. If false, the handler will allow the mocked class's superclass to respond to a method invocation. If true, the handler will respond with a default value (e.g. false if the return type is boolean or 0 if the return type is int).

Parameters:
stubConcreteMethods - Should this handler handle invocations of concrete methods or delegate them to the superclass?
Method Detail

getInvocations

public MethodInvocation[] getInvocations()
A method to get all the invocations that were spied on.

Returns:
The spied on invocations.

getInvocations

public MethodInvocation[] getInvocations(MockMethod method)
A method to get all invocations of a particular method.

Parameters:
method - The method that was spied on.
Returns:
The invocations of the method of interest.

getExpectation

public InvocationVerifier getExpectation(MockMethod method)
A method to get a verifier for checking that a method was invoked as expected.

Parameters:
method - The method of interest.
Returns:
A verifier for validating the spied-on method invocations.
Since:
Hammock 2.0

getInvocations

public MethodInvocation[] getInvocations(MockMethod method,
                                         IMockObject mockObject)
A method to get the invocations of a particular method on a specific (mock) object.

Parameters:
method - The method of interest.
mockObject - The object of interest.
Returns:
The invocations of the method on the object.

getExpectation

public InvocationVerifier getExpectation(MockMethod method,
                                         IMockObject mockObject)
A method to get a verifier for checking that a method was invoked as expected.

Parameters:
method - The method of interest.
mockObject - The mock object on which we were spying.
Returns:
A verifier for validating the spied-on method invocations.
Since:
Hammock 2.0

getExpectation

public InvocationVerifier getExpectation(MockMethod method,
                                         IMockObject mockObject,
                                         java.lang.Object[] methodArgs)
A method to get a verifier for checking that a method was invoked as expected.

Parameters:
method - The method of interest.
mockObject - The mock object on which we were spying.
methodArgs - The expected arguments.
Returns:
A verifier for validating the spied-on method invocations.
Since:
Hammock 2.0

getExpectation

public InvocationVerifier getExpectation(MockMethod method,
                                         java.lang.Object[] methodArgs)
A method to get a verifier for checking that a method was invoked as expected.

Parameters:
method - The method of interest.
methodArgs - The expected arguments.
Returns:
A verifier for validating the spied-on method invocations.
Since:
Hammock 2.0

invoke

public void invoke(MethodInvocation mi)
Processes a method invocation on a spy object. The method invocation is recorded. If an expectation has been set for the method invocation, this handler responds as specified in the expectation. If no, expectation has been set, the handler provides a default response (e.g. returning false if the return type is a boolean) if the method in the superclass is abstract. If the super class has a concrete implementation of the method, the call is delegated to the super class.

Parameters:
mi - The method invocation.