1. MockRunner

1. MockRunner

MockRunner provides an infrastructure to test code outside of a container (JEE application server)

MockRunner contains classes to mock out dependencies on:

1.1. Example Code

Parcel is a bean/POJO/DTO that represents some item that lives in the database.

package be.ooxs.example.mock.mockrunner;

public class Parcel {
	private String scanCode;
	private int weightGrams = 0;
	private String address1 = "";
	private String address2 = "";
	private String address3 = "";
	
//getters and setters...

}
	  			

ParcelManager is some class that accesses a database to implement some business logic.

To make it fit for testing, you need to be able to inject the mock connection later. We do this here with the setConnection method.

package be.ooxs.example.mock.mockrunner;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ParcelManager {
	private Connection connection;

	public Connection getConnection() {
		return connection;
	}

	public void setConnection(Connection connection) {
		this.connection = connection;
	}

	public List<Parcel> findSmallParcels() {
		List<Parcel> result = new ArrayList<Parcel>();
		try {
			String sql = "SELECT scan,weight,address1,address2,address3 FROM Parcels WHERE weight < 500";
			ResultSet rs = connection.createStatement().executeQuery(sql);//(1)
			//ResultSet rs = connection.prepareStatement(sql).executeQuery();//(2)
			//ResultSet rs = connection.prepareCall(sql).executeQuery();//(3)
			while (rs.next()) {
				Parcel parcel = new Parcel();
				parcel.setScanCode(rs.getString("scan"));
				parcel.setWeightGrams(rs.getInt("weight"));
				parcel.setAddress1(rs.getString("address1"));
				parcel.setAddress2(rs.getString("address2"));
				parcel.setAddress3(rs.getString("address3"));
				result.add(parcel);
			}
		} catch (SQLException exception) {
			exception.printStackTrace();
		}
		return result;
	}
}
	  			

UTestParcelManager is the actual test class.

There is one gotcha you should be aware of: depending on how the ResultSet is obtained - via a Statement/PreparedStatement/CallableStatement - a different resultset handler must be used in the test class. Failing to do so will just return a null value for the resultset. The code contains the 2 other options in comment.

package be.ooxs.example.mock.mockrunner;

import java.sql.Connection;
import java.util.List;

import com.mockrunner.jdbc.BasicJDBCTestCaseAdapter;
import com.mockrunner.jdbc.CallableStatementResultSetHandler;
import com.mockrunner.jdbc.PreparedStatementResultSetHandler;
import com.mockrunner.jdbc.StatementResultSetHandler;
import com.mockrunner.mock.jdbc.MockConnection;
import com.mockrunner.mock.jdbc.MockResultSet;

public class UTestParcelManager extends BasicJDBCTestCaseAdapter {
	private Connection jdbcConnection;

	protected void setUp() {
		MockConnection connection = getJDBCMockObjectFactory().getMockConnection();
		StatementResultSetHandler resultSetHandler = connection.getStatementResultSetHandler(); //(1)
		//PreparedStatementResultSetHandler resultSetHandler = connection.getPreparedStatementResultSetHandler(); //(2)
		//CallableStatementResultSetHandler resultSetHandler = connection.getCallableStatementResultSetHandler(); //(3)
		jdbcConnection = connection;

		MockResultSet resultSet = resultSetHandler.createResultSet();
		resultSet.addColumn("scan");
		resultSet.addColumn("address1");
		resultSet.addColumn("address2");
		resultSet.addColumn("address3");
		resultSet.addColumn("weight");
		resultSet.addRow(new Object[] { "AAA123", "Mr. AA", "aa", "aaa", new Integer(496) });
		resultSet.addRow(new Object[] { "BBB456", "Mr. BB", "bb", "bbb", new Integer(325) });

		resultSetHandler.prepareResultSet("SELECT scan,weight,address1,address2,address3 FROM Parcels WHERE weight < 500", resultSet);
	}

	public void testFindSmallParcels() {
		ParcelManager instance = new ParcelManager();
		instance.setConnection(jdbcConnection);
		List<Parcel> result = instance.findSmallParcels();
		assertEquals(2, result.size());
		Parcel parcel = result.get(0);
		assertEquals("AAA123", parcel.getScanCode());
		assertEquals("Mr. AA", parcel.getAddress1());
		assertEquals("aa", parcel.getAddress2());
		assertEquals("aaa", parcel.getAddress3());
		assertEquals(496, parcel.getWeightGrams());
	}
}