biz.xsoftware.mock.client
Class JsMockSuperclass

java.lang.Object
  extended by biz.xsoftware.mock.client.JsMockSuperclass
All Implemented Interfaces:
JsMockObject

public abstract class JsMockSuperclass
extends Object
implements JsMockObject

This is a super class for mock Objects. It has the following options

  1. Guarantee order of events and that events happen on one object
  2. Guarantee events called with order not mattering on one object
  3. Guarantee order of events is correct between two objects(One mock obj. implements two interfaces)
This class will also return the parameters that were passed into the MockObject so they can be introspected in testing to make sure they were correct. The MockObject extending this class can also be told to throw exceptions on certain methods so we can test error leg behavior. Example of how to use MockActionListener implements ActionListener and extends this class The only method in MockActionListener is
 public final static ACTION_METHOD = "actionPerformed method";
 public void actionPerformed(ActionEvent evt) {
     super.methodCalled(ACTION_METHOD, evt);
 }
 
In the test, when you expect an ActionEvent, you can call
 Object o = MockActionListener.expectEvent(ACTION_METHOD);
 ActionEvent evt = (ActionEvent)evt;
 assertNonNull(evt.getSource());
 
Another useful behavior is throwing any type of exception using setExceptionOnMethod(String method, Throwable e). This can test robustness in a system to make sure listeners or services that throw exceptions don't affect your system, or at least affect your system in the proper way.

Author:
Dean Hiller (dean@xsoftware.biz)

Field Summary
static int DEFAULT_WAIT_TIME
          Default wait time to wait for a method to be called once expectCall is called.
 
Fields inherited from interface biz.xsoftware.mock.client.JsMockObject
ANY, NONE
 
Constructor Summary
JsMockSuperclass()
          Default constructor of superclass of all mockObjects with a delay of 10 seconds.
JsMockSuperclass(int delay)
          The constructor to use to override the default delay(DEFAULT_WAIT_TIME) such that the mock object will give methods a longer time to be called before timing out to fail the test.
JsMockSuperclass(String id)
           
 
Method Summary
 void addBehavior(String method, JsBehavior behavior)
           
 void addIgnore(String method)
          When calling expect, the MockObject will ignore this method, so it will not result in an exception.
 void addReturnValue(String method, Object o)
          Add a return value to return when 'method' is called.
 void addThrowException(String method, Throwable e)
          Add an exception to throw when a method on the mockObject is called.
 JsCalledMethod expect(String method)
          Waits for one and only one method to be called.
 JsCalledMethod[] expect(String[] methods)
          Waits for all the methods to be called.
 int getExpectTimeout()
           
 void removeIgnore(String method)
          Removes the method from the ignored methods set.
 void setDefaultReturnValue(String method, Object o)
          Set the DefaultReturnValue for a 'method'
 void setExpectTimeout(int delay)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_WAIT_TIME

public static final int DEFAULT_WAIT_TIME
Default wait time to wait for a method to be called once expectCall is called.

See Also:
Constant Field Values
Constructor Detail

JsMockSuperclass

public JsMockSuperclass()
Default constructor of superclass of all mockObjects with a delay of 10 seconds. This delay is the amount of time the mock object waits for a method to be called when a client calls expectCall.


JsMockSuperclass

public JsMockSuperclass(int delay)
The constructor to use to override the default delay(DEFAULT_WAIT_TIME) such that the mock object will give methods a longer time to be called before timing out to fail the test.

Parameters:
delay - The amount of time in milliseconds to wait for a method to be called.

JsMockSuperclass

public JsMockSuperclass(String id)
Method Detail

setExpectTimeout

public void setExpectTimeout(int delay)
Specified by:
setExpectTimeout in interface JsMockObject

getExpectTimeout

public int getExpectTimeout()
Specified by:
getExpectTimeout in interface JsMockObject

addIgnore

public void addIgnore(String method)
Description copied from interface: JsMockObject
When calling expect, the MockObject will ignore this method, so it will not result in an exception.

Specified by:
addIgnore in interface JsMockObject

removeIgnore

public void removeIgnore(String method)
Description copied from interface: JsMockObject
Removes the method from the ignored methods set.

Specified by:
removeIgnore in interface JsMockObject

expect

public JsCalledMethod expect(String method)
Description copied from interface: JsMockObject
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.

Specified by:
expect in interface JsMockObject
Parameters:
method - The expected method.
Returns:
An array of params that were passed to the methods called
See Also:
biz.xsoftware.mock.JsMockObject#expect(java.lang.String)

expect

public JsCalledMethod[] expect(String[] methods)
Description copied from interface: JsMockObject
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.

Specified by:
expect in interface JsMockObject
Parameters:
methods - The expected method(s) in the correct order.
Returns:
An array or arrays of params that were passed to the methods called
See Also:
biz.xsoftware.mock.JsMockObject#expect(java.lang.String[])

addThrowException

public void addThrowException(String method,
                              Throwable e)
Description copied from interface: JsMockObject
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

Specified by:
addThrowException in interface JsMockObject
Parameters:
method - The method to throw the exception on when it is called.
e - The exception to throw on method.
See Also:
biz.xsoftware.mock.JsMockObject#addThrowException(java.lang.String, java.lang.Throwable)

addReturnValue

public void addReturnValue(String method,
                           Object o)
Description copied from interface: JsMockObject
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 defaultvalue which is null unless setDefaultReturnValue is called on MockObject.

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

Specified by:
addReturnValue in interface JsMockObject
Parameters:
method - The method that when called returns first value on queue
o - The object to return that is added to the queue
See Also:
biz.xsoftware.mock.JsMockObject#addReturnValue(java.lang.String, java.lang.Object)

setDefaultReturnValue

public void setDefaultReturnValue(String method,
                                  Object o)
Description copied from interface: JsMockObject
Set the DefaultReturnValue for a 'method'

Specified by:
setDefaultReturnValue in interface JsMockObject
Parameters:
method - The method

addBehavior

public void addBehavior(String method,
                        JsBehavior behavior)
Specified by:
addBehavior in interface JsMockObject


Copyright © 2000 Dean Hiller All Rights Reserved.
If you would like a shared copyright, contact me at deanhiller@users.sourceforge.net
SourceForge