biz.xsoftware.mock
Interface MockObject


public interface MockObject

The interface all mock objects implement. This is the interface used by the unit tests once the mock object is created.

Author:
Dean Hiller

Field Summary
static String ANY
          Field used to expect any method so expect returns as soon as any method is called.
static String NONE
          Field used to expect that no methods have been called.
 
Method Summary
 void addBehavior(MethodSignature methodSignature, Behavior behavior)
          Adds a snippet of code to be run which exists in the Behavior implementation.
 void addBehavior(String method, Behavior behavior)
          Adds a snippet of code to be run which exists in the Behavior implementation.
 void addBehavior(String method, Behavior behavior, Class<?>... argTypes)
          Deprecated. Since 2007 Aug 8. Use addBehavior(MethodSignature, Behavior) instead
 void addIgnore(MethodSignature... methodSignature)
          When calling expect, the MockObject will ignore the methods in 'methods' variable so if one of the methods in this array is called, it will not result in an exception.
 void addIgnore(String... method)
          When calling expect, the MockObject will ignore the methods in 'methods' variable so if one of the methods in this array is called, it will not result in an exception
<T> void
addReturnValue(MethodSignature methodSignature, T... o)
          Add a return value to return when 'method' is called.
<T> void
addReturnValue(Method method, T... o)
          Add a return value to return when 'method' is called.
 void addReturnValue(String method, boolean isArray, Object... o)
          Deprecated. addReturnValue(Method, Object...) was enhanced to detect if the given method returns an array. If it does then the object array passed in will be the object returned for a single method call
<T> void
addReturnValue(String method, T... o)
          Add a return value to return when 'method' is called.
 void addThrowException(MethodSignature method, Throwable e)
          Add an exception to throw when a method on the mockObject is called.
 void addThrowException(String method, Throwable e)
          Add an exception to throw when a method on the mockObject is called.
 CalledMethod[] expect(String... methods)
          Waits for all the methods to be called.
 CalledMethod expect(String method)
          Waits for one and only one method to be called.
 CalledMethod[] expect(String method, int times)
          Waits for a methods to be called a specific number of times.
 CalledMethod expectCall(String method)
          Deprecated. Use expect(String) now
 CalledMethod expectCall(String method, String... ignoredMethods)
          Deprecated. Use a combination of addIgnore(String[]) and expect(String)
 CalledMethod[] expectOnceThenIgnore(String... methods)
          This method will expect the given method one time and ignore any further calls to the method.
 CalledMethod[] expectOrderedCalls(String... methods)
          Deprecated. Use expect(String[]) instead
 CalledMethod[] expectOrderedCalls(String[] methods, String[] ignoredMethods)
          Deprecated. This is no longer supported, the use of addIgnore(String[]) in combination with expect(String[]) should be used
 CalledMethod[] expectUnordered(String... methods)
          Expects the given methods to be called in no particular order
 CalledMethod expectUnordered(String method)
           
 CalledMethod[] expectUnorderedCalls(String... methods)
          Deprecated. No longer supported
 CalledMethod[] expectUnorderedCalls(String[] methods, String[] ignoredMethods)
          Deprecated. This is no longer supported, the use of addIgnore(String[]) in combination with expect(String[]) should be used
 int getExpectTimeout()
          Gets the timeout which is the delay that a mockobject waits for a method to be called before failing.
 boolean isCaseSensitive()
          Returns the current setting for case sensitivity
 void removeIgnore(MethodSignature... methodSignature)
          Removes the method from the ignored methods set.
 void removeIgnore(String... methods)
          Removes the method(s) from the ignored methods set
 void reset()
          This method will reset the MockObject to an empty state.
 void setBehavior(Behavior behavior)
          This method will look at the given behavior and will automatically call setDefaultBehavior(String, Behavior, Class[]) for all the methods that have the BehaviorMethod annotation
 void setCaseSensitive(boolean isCaseSensitive)
          This is used if you want to ignore case sensitivity.
 void setDefaultBehavior(MethodSignature methodSignature, Behavior b)
          Set the DefaultBehavior for a 'method'.
 void setDefaultBehavior(String method, Behavior b)
          Set the DefaultBehavior for a 'method'.
 void setDefaultBehavior(String method, Behavior b, Class<?>... argTypes)
          Deprecated. Since 2007 Aug 8. Use setDefaultBehavior(MethodSignature, Behavior) instead
 void setDefaultReturnValue(Method method, Object returnValue)
          Provide the return value for a specific Method.
 void setDefaultReturnValue(String method, Object returnValue)
          Set the DefaultReturnValue for a 'method'.
 void setExpectTimeout(int timeout)
          Sets the timeout which is the delay that a mockobject waits for a method to be called before failing.
 

Field Detail

NONE

static final String NONE
Field used to expect that no methods have been called.

See Also:
Constant Field Values

ANY

static final String ANY
Field used to expect any method so expect returns as soon as any method is called.

See Also:
Constant Field Values
Method Detail

expect

CalledMethod expect(String method)
Waits for one and only one method to be called. If any methods are called before or after this one(after can sometimes be caught by the MockObject when the threading is not synchronous), then this call will throw an ExpectFailedException.

Parameters:
method - The expected method.
Returns:
An array of params that were passed to the methods called

expect

CalledMethod[] expect(String... methods)
Waits for all the methods to be called. If any of the methods are not called or are called out of order, this method throws an exception which will fail the test case. For each method, this will wait WAIT_TIME milliseconds for each method to be called. If the method is not called within this period, an exception will be thrown saying 'method' was not called within timeout period.

Parameters:
methods - The expected method(s) in the correct order.
Returns:
An array or arrays of params that were passed to the methods called

expect

CalledMethod[] expect(String method,
                      int times)
Waits for a methods to be called a specific number of times. If any of the methods, this method throws an exception which will fail the test case. For each method, this will wait WAIT_TIME milliseconds for each method to be called. If the method is not called within this period, an exception will be thrown saying 'method' was not called within timeout period.

Parameters:
method -
times - is the number of times the methods is expected to be called
Returns:
An array or arrays of params that were passed to the methods called

expectUnordered

CalledMethod expectUnordered(String method)

expectUnordered

CalledMethod[] expectUnordered(String... methods)
Expects the given methods to be called in no particular order

Parameters:
methods - The methods to expect
Returns:
The methods that were called with their parameters

expectOnceThenIgnore

CalledMethod[] expectOnceThenIgnore(String... methods)
This method will expect the given method one time and ignore any further calls to the method. This is useful for cases where the method maybe called many times, but you just want to verify it happened at least once

Parameters:
methods - The method names to ignore
Returns:
The information about the method called

expectCall

@Deprecated
CalledMethod expectCall(String method)
Deprecated. Use expect(String) now


expectCall

CalledMethod expectCall(String method,
                        String... ignoredMethods)
Deprecated. Use a combination of addIgnore(String[]) and expect(String)

This method will call addIgnore for the given methods, run expect with the given method and then remove all of the ignored methods


expectOrderedCalls

@Deprecated
CalledMethod[] expectOrderedCalls(String... methods)
Deprecated. Use expect(String[]) instead


expectOrderedCalls

@Deprecated
CalledMethod[] expectOrderedCalls(String[] methods,
                                             String[] ignoredMethods)
Deprecated. This is no longer supported, the use of addIgnore(String[]) in combination with expect(String[]) should be used


expectUnorderedCalls

@Deprecated
CalledMethod[] expectUnorderedCalls(String... methods)
Deprecated. No longer supported


expectUnorderedCalls

@Deprecated
CalledMethod[] expectUnorderedCalls(String[] methods,
                                               String[] ignoredMethods)
Deprecated. This is no longer supported, the use of addIgnore(String[]) in combination with expect(String[]) should be used

Calls addIgnore(String[]) with the given ignored methods, then runs expectUnorderedCalls(String[]), saving the result. Then removes the ignored methods and finally returns the saved result


setDefaultReturnValue

void setDefaultReturnValue(String method,
                           Object returnValue)
Set the DefaultReturnValue for a 'method'. This value is returned if none of the following happen
  1. addReturnValue is not called giving a return value
  2. addThrowException is not called returning an exception
  3. addBehavior is not called running a snippet of code that returns a value

Parameters:
method - The method name to set the default return value for
returnValue -

setDefaultReturnValue

void setDefaultReturnValue(Method method,
                           Object returnValue)
Provide the return value for a specific Method. This is useful for specifying a method that you're having trouble getting MockObject to handle correctly.

For example: MockObject m = MockObjectFactory.createMock(Runnable.class); Method method = Object.class.getMethod("toString"); m.setDefaultReturnValue(method, "test"); assertEquals("test", m.toString());

Parameters:
method - The method to tie the return value to.
returnValue - The value to return when the method is called

setDefaultBehavior

void setDefaultBehavior(String method,
                        Behavior b)
Set the DefaultBehavior for a 'method'. This Behavior object is used to run a snippet of code if none of the following happen
  1. addReturnValue is not called giving a return value
  2. addThrowException is not called returning an exception
  3. addBehavior is not called running a snippet of code that returns a value
See the Behavior class for more info on what to put in the Behavior implementation.

Parameters:
method - When this method is called, the Behavior will be run.
b - The snippet of code to be run.

setDefaultBehavior

@Deprecated
void setDefaultBehavior(String method,
                                   Behavior b,
                                   Class<?>... argTypes)
Deprecated. Since 2007 Aug 8. Use setDefaultBehavior(MethodSignature, Behavior) instead

Set the DefaultBehavior for a 'method'. This Behavior object is used to run a snippet of code if none of the following happen
  1. addReturnValue is not called giving a return value
  2. addThrowException is not called returning an exception
  3. addBehavior is not called running a snippet of code that returns a value
A Behavior or CloningBehavior can be passed in. See the Behavior or CloningBehavior class for more info on what to put in the Behavior implementation.

Parameters:
method - When this method is called, the Behavior will be run.
b - The snippet of code to be run.
argTypes - The parameters types of the method so overloaded method can be specified

setDefaultBehavior

void setDefaultBehavior(MethodSignature methodSignature,
                        Behavior b)
Set the DefaultBehavior for a 'method'. This Behavior object is used to run a snippet of code if none of the following happen
  1. addReturnValue is not called giving a return value
  2. addThrowException is not called returning an exception
  3. addBehavior is not called running a snippet of code that returns a value
A Behavior or CloningBehavior can be passed in. See the Behavior or CloningBehavior class for more info on what to put in the Behavior implementation.

Parameters:
methodSignature - Used to define which method the behavior should be assigned to
b - The snippet of code to be run.

addThrowException

void addThrowException(String method,
                       Throwable e)
Add an exception to throw when a method on the mockObject is called.
This can be called many times where each time it is called, the exception is added to a queue. Each time 'method' is called, it checks the queue, if empty, it does not throw an exception.
Should only pass the type of Throwable that the 'method' can throw. If you pass IOException in for a method that doesn't have a signature of 'throws IOException', then one of the following exceptions will be thrown into the sysUnderTest

This method only expects the method name. It will search for a method name that matches ignoring any method parameters. If more than one methods are found then an exception will be thrown. In this case use addThrowException(MethodSignature, Throwable) instead

  1. UndeclaredThrowableException for MockObjects created with MockCreator
  2. RuntimeException for Subclasses of MockSuperclass

Parameters:
method - The method to throw the exception on when it is called.
e - The exception to throw on method.

addThrowException

void addThrowException(MethodSignature method,
                       Throwable e)
Add an exception to throw when a method on the mockObject is called.

This can be called many times where each time it is called, the exception is added to a queue. Each time 'method' is called, it checks the queue, if empty, it does not throw an exception.
Should only pass the type of Throwable that the 'method' can throw. If you pass IOException in for a method that doesn't have a signature of 'throws IOException', then one of the following exceptions will be thrown into the sysUnderTest

  1. UndeclaredThrowableException for MockObjects created with MockCreator
  2. RuntimeException for Subclasses of MockSuperclass

Parameters:
method -
e -

addReturnValue

<T> void addReturnValue(String method,
                        T... o)
Add a return value to return when 'method' is called.

This can be called multiple times and each call will add to a queue. When 'method' is called, it will return the first value on the queue. If the queue is null, 'method' will return the default value which is null unless setDefaultReturnValue is called on MockObject.

Use Integer to return int, Long for long, etc.

Parameters:
method - The method that when called returns first value on queue
o - The object or objects to return that is added to the queue

addReturnValue

<T> void addReturnValue(MethodSignature methodSignature,
                        T... o)
Add a return value to return when 'method' is called.

This can be called multiple times and each call will add to a queue. When 'method' is called, it will return the first value on the queue. If the queue is null, 'method' will return the default value which is null unless setDefaultReturnValue is called on MockObject.

Use Integer to return int, Long for long, etc.


addReturnValue

<T> void addReturnValue(Method method,
                        T... o)
Add a return value to return when 'method' is called.

This can be called multiple times and each call will add to a queue. When 'method' is called, it will return the first value on the queue. If the queue is null, 'method' will return the default value which is null unless setDefaultReturnValue is called on MockObject.

Use Integer to return int, Long for long, etc.


addReturnValue

@Deprecated
void addReturnValue(String method,
                               boolean isArray,
                               Object... o)
Deprecated. addReturnValue(Method, Object...) was enhanced to detect if the given method returns an array. If it does then the object array passed in will be the object returned for a single method call


setBehavior

void setBehavior(Behavior behavior)
This method will look at the given behavior and will automatically call setDefaultBehavior(String, Behavior, Class[]) for all the methods that have the BehaviorMethod annotation

Parameters:
behavior -

addBehavior

void addBehavior(String method,
                 Behavior behavior)
Adds a snippet of code to be run which exists in the Behavior implementation. A Behavior or CloningBehavior can be passed in. See the Behavior or CloningBehavior class for more info on what to put in the Behavior implementation.

Parameters:
method - When this method is called, the Behavior will be run
behavior - The snippet of code to be run.

addBehavior

@Deprecated
void addBehavior(String method,
                            Behavior behavior,
                            Class<?>... argTypes)
Deprecated. Since 2007 Aug 8. Use addBehavior(MethodSignature, Behavior) instead

Adds a snippet of code to be run which exists in the Behavior implementation. A Behavior or CloningBehavior can be passed in. See the Behavior or CloningBehavior class for more info on what to put in the Behavior implementation.

Parameters:
method - When this method is called, the Behavior will be run
behavior - The snippet of code to be run.
argTypes - The parameters to the method to distinguish between overloaded methods.

addBehavior

void addBehavior(MethodSignature methodSignature,
                 Behavior behavior)
Adds a snippet of code to be run which exists in the Behavior implementation. A Behavior or CloningBehavior can be passed in. See the Behavior or CloningBehavior class for more info on what to put in the Behavior implementation.

Parameters:
methodSignature - Used to define which method the behavior should be assigned to
behavior - The snippet of code to be run.

addIgnore

void addIgnore(String... method)
When calling expect, the MockObject will ignore the methods in 'methods' variable so if one of the methods in this array is called, it will not result in an exception

Parameters:
method - The name of the method(s) to ignore

addIgnore

void addIgnore(MethodSignature... methodSignature)
When calling expect, the MockObject will ignore the methods in 'methods' variable so if one of the methods in this array is called, it will not result in an exception.

Parameters:
methodSignature - The MethodSignature to add to the list of ignored methods

removeIgnore

void removeIgnore(String... methods)
Removes the method(s) from the ignored methods set

Parameters:
methods - The name of the method(s) to remove from the list of ignored methods

removeIgnore

void removeIgnore(MethodSignature... methodSignature)
Removes the method from the ignored methods set.

Parameters:
methodSignature - The MethodSignature to remove from the ignore list

setExpectTimeout

void setExpectTimeout(int timeout)
Sets the timeout which is the delay that a mockobject waits for a method to be called before failing. The default is 5 seconds.


getExpectTimeout

int getExpectTimeout()
Gets the timeout which is the delay that a mockobject waits for a method to be called before failing.

Returns:
The current expect timeout duration

setCaseSensitive

void setCaseSensitive(boolean isCaseSensitive)
This is used if you want to ignore case sensitivity. You could then have a method interface.checkWorks() and successfully run mockObj.expect("checkworks")

Parameters:
isCaseSensitive -

isCaseSensitive

boolean isCaseSensitive()
Returns the current setting for case sensitivity


reset

void reset()
This method will reset the MockObject to an empty state. This means it will clear all of the called methods and remove all the ignores that were added. In addition it will remove all of the set return values and behaviors