Generating Mock Objects with MockMaker
Problem
You want to automatically generate a mock object from any Java interface.
Solution
Use MockMaker, available from http://www.mockmaker.org.
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:
java mockmaker.MockMaker javax.swing.event.TableModelListenerExample 6-11. Generated mock object
import mockmaker.ReturnValues; import com.mockobjects.*; import javax.swing.event.TableModelListener; import javax.swing.event.TableModelEvent; public class MockTableModelListener implements TableModelListener{ private ExpectationCounter myTableChangedCalls = new ExpectationCounter("javax.swing. event.TableModelListener TableChangedCalls"); private ExpectationList myTableChangedParameter0Values = new ExpectationList("javax. swing.event.TableModelListener TableChangedParameter0Values"); public void setExpectedTableChangedCalls(int calls){ myTableChangedCalls.setExpected(calls); } public void addExpectedTableChangedValues(TableModelEvent ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access