jface.mockobject
Class MockObjectManager

java.lang.Object
  |
  +--jface.mockobject.MockObjectManager

public class MockObjectManager
extends java.lang.Object

Under certain circumstances, the construction of mock objects is very complicated. Especially when testing classes, that contain deep in their code classes that need to be mocked up. Also classes with lots of external resources need complex mock up structures.

To solve this problem, the MockObjectManager can be used. The class that is tested, needs a special structure to be used with the MockObjectManager, which is described as follows:

Assume, a class, called BigComplicatedClass, has lots of connections to databases and other external resources. All the calls to these collaborators should be extracted into an inner interface, to provide a clear structure for the mockup:

 public class BigComplicatedClass
 {
     Collaborator col;
 
     public BigComplicatedClass()
     {
          col = new DefaultCollaborator();
     }
 
     public String doSomethingComplicated(int something)
     {
         col.writeToDatabase(something);
         return col.readFromServer(); 
     }
 
     public interface Collaborator
     {
         public void writeToDatabase(int value);
         public String readFromServer();
     }
 
     private class DefaultCollaborator implements Collaborator
     {
         public void writeToDatabase(int value)
         {
             // call the database and write the value.
         }
 
         public String readFromServer()
         {
             // call the server and read the value, then return the value.
         }
     }
 }
 

Note, that the field Collaborator col is package protected by intention: It should be accessible by classes in the same package (in fact: it should only be accessible by the test class, but that's impossible in Java), but should be invisible to the user of the class.

Now, in the unit test, we can use the MockObjectManager to create a mock-up version of the Collaborator interface:

 public class BigComplicatedClassTest extends TestCase
 {
     // ...
 
     public void testDoSomethingComplicated()
     {
         // [1]
         MockObjectManager manager = new MockObjectManager();
 
         // [2]
         BigComplicatedClass big = new BigComplicatedClass();
 
         // [3]
         big.col = (Collaborator) manager.newMockObject(Collaborator.class);
 
         // [4]
         big.col.writeToDatabase(10);
         manager.nextReturnValue("Hello World!");
         big.col.readFromServer();
 
         // [5]
         manager.setPlaybackMode(true);
 
         // [6]
         assertEquals("Hello World!", big.doSomethingComplicated(10));
 
         // [7]
         manager.validate();
     }
 }
 

The test code contains of seven steps:

[1]
Create a new MockObjectManager instance.

[2]
Create a new instance of the class to be tested.

[3]
Create a mock-up version of the interface providing the collaborator objects to the tested class. Use the instanciated MockObjectManager instance of step [1] for this.

[4]
Define the expected values and order of the method calls.

[5]
Start the playback mode. The MockObjectManager has been in recording mode before. Now the method calls to the "collaborator" interface expect the values defined in step [4] and return the defined values.

[6]
Now run the actual test code.

[7]
Finally validate the generated mock objects. This ensures, that all recorded methods have been called successfully.

Sometimes recording all steps inside a mockobject is difficult. If the sourcecode of the mocked up object changes, the recording must change, too. So it is desirable to record only the key methods and skip other methods. This can be achieved by the nextIgnoreMethods(boolean) method. If set to true all methods are ignored until the next recorded method is reached.

In this case it is possible, that the method parameters are undefined. Use nextIgnoreParameters(boolean) to ignore the parameter values.

Version:
$Id: MockObjectManager.java,v 1.6 2004/01/07 22:43:41 powerpete Exp $
Author:
Moritz Petersen

Constructor Summary
MockObjectManager()
           
 
Method Summary
 java.lang.Object newMockObject(java.lang.Class theInterface)
          Creates a new mock object implementing the given interface.
 java.lang.Object newMockObject(java.lang.Class[] interfaces)
          Creates a new mock object implementing the given interfaces.
 void nextIgnoreMethods(boolean ignoreMethods)
          Sets the mockobject to ignore the next methods.
 void nextIgnoreParameters(boolean ignoreParameters)
          Sets the mockobject to ignore the next method parameters.
 void nextReturnValue(boolean b)
          Registers the given boolean as next return value.
 void nextReturnValue(byte b)
          Registers the given byte as next return value.
 void nextReturnValue(double d)
          Registers the given double as next return value.
 void nextReturnValue(float f)
          Registers the given float as next return value.
 void nextReturnValue(int i)
          Registers the given int as next return value.
 void nextReturnValue(long l)
          Registers the given long as next return value.
 void nextReturnValue(java.lang.Object o)
          Registers the given Object as next return value.
 void nextReturnValue(short s)
          Registers the given short as next return value.
 void nextThrowable(java.lang.Throwable t)
          Registers the next Throwable.
 void setPlaybackMode(boolean b)
          Toggles the playback mode.
 void validate()
          Validates all generated mockobjects.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockObjectManager

public MockObjectManager()
Method Detail

newMockObject

public java.lang.Object newMockObject(java.lang.Class[] interfaces)
Creates a new mock object implementing the given interfaces.

Parameters:
interfaces - A number of interfaces that is implemented by the returned mock object.
Returns:
the mock object.

newMockObject

public java.lang.Object newMockObject(java.lang.Class theInterface)
Creates a new mock object implementing the given interface. This method is a shortcut for newMockObject(new Class[] {MyInterface.class}) .

Parameters:
theInterface - The implementing interface of the returned mock object.
Returns:
the mock object.

nextReturnValue

public void nextReturnValue(boolean b)
Registers the given boolean as next return value.

Parameters:
b - The next return value.

nextReturnValue

public void nextReturnValue(byte b)
Registers the given byte as next return value.

Parameters:
b - The next return value.

nextReturnValue

public void nextReturnValue(double d)
Registers the given double as next return value.

Parameters:
d - The next return value.

nextReturnValue

public void nextReturnValue(float f)
Registers the given float as next return value.

Parameters:
f - The next return value.

nextReturnValue

public void nextReturnValue(int i)
Registers the given int as next return value.

Parameters:
i - The next return value.

nextReturnValue

public void nextReturnValue(long l)
Registers the given long as next return value.

Parameters:
l - The next return value.

nextReturnValue

public void nextReturnValue(short s)
Registers the given short as next return value.

Parameters:
s - The next return value.

nextReturnValue

public void nextReturnValue(java.lang.Object o)
Registers the given Object as next return value.

Parameters:
o - The next return value.

nextThrowable

public void nextThrowable(java.lang.Throwable t)
Registers the next Throwable.

Parameters:
t - The next Throwable.

setPlaybackMode

public void setPlaybackMode(boolean b)
Toggles the playback mode. If b is true the mode is set to "playback" and the method calls to the generated mock objects test their parameters and return the registered values. If the mode is not "playback" it is "recording", which is default. In this mode, the calls to the methods of the generated mock objects record the values for later playback.

Parameters:
b - If true the mode is set to playback, otherwise it is set to recording, which is default mode.

validate

public void validate()
Validates all generated mockobjects.


nextIgnoreMethods

public void nextIgnoreMethods(boolean ignoreMethods)
Sets the mockobject to ignore the next methods.

Parameters:
ignoreMethods - If true the next methods are ignored until the expected method is reached.

nextIgnoreParameters

public void nextIgnoreParameters(boolean ignoreParameters)
Sets the mockobject to ignore the next method parameters.

Parameters:
ignoreParameters - If true the parameter values of the next method are ignored and not checked.


Copyright © 2003-2004 jface.org. All Rights Reserved.