6.3 Mock Object Self-Validation

6.3.1 Problem

You want to avoid duplicated validation logic in your tests.

6.3.2 Solution

Put the validation logic inside of the mock object. This way, every test that uses the mock object will reuse the validation logic automatically.

6.3.3 Discussion

The code in the previous recipe showed how to create a mock table model listener that kept track of a list of events. As you write more tests using this mock object, you will find that your tests have to repeatedly check the number of events as well as every field within the event objects. Rather than repeating this logic in each of your tests, move some of the validation logic into the mock object. Example 6-5 shows how this step simplifies your tests.

Example 6-5. Improved unit test
public void testAddAccountEvent(  ) {
    MockTableModelListener mockListener = new MockTableModelListener(  );
    TableModelEvent evt = new TableModelEvent(


    this.acctTableModel.addAccount(new Account(
            Account.CHECKING, "12345", 100.50));

    mockListener.verify(  );

The modified unit test begins by setting the expected event count on the improved mock object. The mock object will fail the test as soon as it receives too many events. This is useful because it lets you see test failures as soon as the extra events are delivered, making diagnosis easier.

The test also registers a specific expected event. Once the account is added to the table model, the test calls verify( ), which tests against the expected event. Example 6-6 shows the new, improved mock object.

Example 6-6. Self-validating mock listener
package com.oreilly.mock;

import junit.framework.Assert;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import java.util.ArrayList;
import java.util.List;

public class MockTableModelListener implements TableModelListener {
    private static final int NONE_EXPECTED = -1;
    private List events = new ArrayList(  );
    private List expectedEvents = null;
    private int expectedEventCount = NONE_EXPECTED;

    public void addExpectedEvent(TableModelEvent e) {
        if (this.expectedEvents == null) {
            this.expectedEvents = new ArrayList(  );
        this.expectedEvents.add(new ComparableTableModelEvent(e));

    public void setExpectedEventCount(int n) {
        this.expectedEventCount = n;

    public void tableChanged(TableModelEvent e) {
        if (this.expectedEventCount > NONE_EXPECTED
                && this.events.size(  ) > this.expectedEventCount) {
            Assert.fail("Exceeded the expected event count: "
                    + this.expectedEventCount);

    public int getEventCount(  ) {
        return this.events.size(  );

    public List getEvents(  ) {
        return this.events;

    public void verify(  ) {
        if (this.expectedEventCount > NONE_EXPECTED) {
            Assert.assertEquals("Expected event count",
                    this.events.size(  ));

        if (this.expectedEvents != null) {
            Assert.assertEquals("Expected events",

    class ComparableTableModelEvent extends TableModelEvent {
        public ComparableTableModelEvent(TableModelEvent orig) {
            super((TableModel) orig.getSource(), orig.getFirstRow(  ),
                    orig.getLastRow(), orig.getColumn(), orig.getType(  ));

        public boolean equals(Object obj) {
            TableModelEvent tm = (TableModelEvent) obj;
            return getSource() == tm.getSource(  )
                    && getFirstRow() == tm.getFirstRow(  )
                    && getLastRow() == tm.getLastRow(  )
                    && getColumn() == tm.getColumn(  )
                    && getType() == tm.getType(  );

As you can see, the mock object is significantly more complex in this approach. Only write sophisticated mock objects when you find yourself using them in a lot of different tests. As is customary in an XP approach, start simple and then refactor the tests and mock objects as you observe duplicated code.

Our mock object illustrates an interesting point about JUnit. The methods in the junit.framework.Assert class are static, so we can call them from our mock object, which is not itself a unit test:

Assert.fail("Exceeded the expected event count: "
        + this.expectedEventCount);

6.3.4 See Also

Recipe 6.7 shows how to autogenerate complex mock objects using MockMaker.