Writing your own mock procedures

The SQLUnitMockDatabase provides various examples of mock procedures written using the SQLUnit Mock framework, but there are a number of conventions which you should be aware of when reading through these examples.

  1. Each mock procedure will need to return various resultsets based on the resultset id supplied. Calls made to the same procedure with different values of the resultset id will result in different MockResultSet objects being returned.

  2. ResultSet 0 is always the count of the number of resultsets that can be returned from this procedure call, embedded in a MockResultSet object.

  3. ResultSet 1 through n represents the first through the n-th ResultSet objects that may be returned from the procedure.

  4. ResultSet -1 through -n represents the first through n-th Outparam objects that may be returned from the procedure. They need to be always specified as Strings, they will be automatically converted based on the Outparam definition supplied in the param element. Oracle CURSOR outparams are returned as MockResultSets.

  5. Throwing of SQLExceptions can be simulated by wrapping a SQLException within a MockResultSet at a particular ResultSet position.

  6. MockResultSetUtils contains most of the functionality of wrapping and unwrapping non-ResultSet objects into and out of MockResultSets. Consider adding functionality to this class if you do not see something you want.

Here is an example of a mock procedure that returns a single return code (OUTPARAM 1) and a single resultset consisting of 1 row with 3 columns. As described above, resultset index of -1 corresponds to the return code at outparam position 1, resultset index of 0 corresponds to the number of resultsets returned from the procedure, and resultset index of 1 corresponds to the first resultset returned from the procedure.


/**
 * Returns a result code and a single resultset.
 * @param index the result set id.
 * @return a MockResultSet at the specified index.
 */
public MockResultSet resultAndOneResultSet(Integer index) {
    if (index == null) { return null; }
    int rsid = index.intValue();
    switch (rsid) {
        case -1:
            return MockResultSetUtils.buildScalarOutParam("143");
        case 0:
            return MockResultSetUtils.buildZerothResultSet(1);
        case 1:
            MockResultSet mrs = new MockResultSet("resultAnd1ResultSet:-1");
            mrs.setResultSetMetaData(MockResultSetUtils.buildMetaData(
                new ColumnMetaData[] {
                new ColumnMetaData("agentId", Types.INTEGER),
                new ColumnMetaData("name", Types.VARCHAR),
                new ColumnMetaData("drink", Types.VARCHAR)}));
            mrs.addRow(new Object[] {
                new Integer(7), new String("James Bond"),
                new String("Martini")});
            return mrs;
        default:
            return null;
    }
}
      

More information on how the various peices fit together from a programmatic perspective can be found in the package level documentation in the Javadocs for this package.