6.6 Generating Mock Objects with MockMaker

6.6.1 Problem

You want to
automatically
generate a mock object from any Java interface.

6.6.2 Solution

Use MockMaker, available from
http://www.mockmaker.org.

6.6.3 Discussion

Writing mock objects by hand is tedious, and relying on a framework
like Mock Objects is troublesome because it might not provide mock
implementations for all of the interfaces you need to test against.
The MockMaker project allows you to automatically generate new mock
objects from any existing Java interface.

Using MockMaker is simple. Just include the MockMaker JAR files in
your CLASSPATH and invoke the tool as follows:

java mockmaker.MockMaker <interfaceName>

The generated source code is then echoed to the console. Example 6-11 shows the output from typing the
following command:

The generated code relies on code found in the Mock Objects framework
for keeping track of expectations, such as the expected events or
number of times a method was called. You use this class almost
exactly like you would use the hand-coded mock object, as shown in
Example 6-5 (although the method names are slightly
different).

6.6.4 See Also

Recipe 6.2 and Recipe 6.3
show how to hand-code mock objects that look similar to the code
generated by MockMaker. The Mock Objects web site, http://www.mockobjects.com, lists URLs for
several other mock object generation tools, including Easy Mock, Mock
Creator, and Mock Doclet.