net.sourceforge.sqlunit.test.mock |
Mock Testing framework for SQLUnit. Provides a framework to test SQLUnit code without a database. The database independence is achieved by using the MockRunner JDBC framework.
Instead of a real database, the testing framework relies on introspecting a specified class. The class implements the IMockDatabase interface and contains methods with the signature:
public MockResultSet methodName(Integer resultSetId);
The IMockDatabase interface mandates a getResultSet(String,int) method which returns a MockResultSet. The implementation of this method is provided in the AbstractMockDatabase class. Clients wishing to develop their own mock database classes should extend this class.
Examples of methods that mimic the behavior of stored procedures can be found in the SQLUnitMockDatabase class which extends AbstractMockDatabase. Things to note are:
- The count of the number of results (used for the Statement.execute() and CallableStatement.getMoreResults()) is returned in wrapped in result set 0.
- Outparam values are generated internally using the negative of the supplied index. Thus, the value of OUTPARAM at position 1 will be wrapped in the resultset that is returned by invoking the appropriate method with -1. Scalar OUTPARAM values are specified as Strings, but are converted to the appropriate Object on retrieval. REF CURSOR OUTPARAM objects are returned as themselves since they are essentially ResultSet objects.
- Exceptions are not thrown directly, but they are wrapped in the resultset that is being made to throw the exception.
- ResultSets are returned from the method using the supplied index. Thus the first ResultSet will be returned by invoking the method with Integer(1).
The functionality of wrapping and unwrapping scalar and SQLException objects back and forth from MockResultSet objects can be found in the MockResultSetUtils class.
To make this work with the MockRunner framework, the following classes were extended from the MockRunner distribution:
- SQLUnitMockConnection extends com.mockrunner.mock.jdbc.MockConnection. The extensions were mainly to call the Introspecting versions of the ResultSetHandler classes instead of MockRunner's ResultSetHandlers.
- SQLUnitMockDriver extends com.mockrunner.mock.jdbc.MockDriver. This version provides a URL signature which the mock framework will respond to.
- SQLUnitMockStatement extends com.mockrunner.mock.jdbc.MockStatement. Calls the Introspecting version of the ResultSetHandler.
- SQLUnitMockPreparedStatement extends com.mockrunner.mock.jdbc.MockPreparedStatement. Calls the Introspecting version of the ResultSetHandler.
- SQLUnitMockCallableStatement extends com.mockrunner.mock.jdbc.MockStatement. Calls the Introspecting version of the ResultSetHandler.
- IntrospectingStatementResultSetHandler extends com.mockrunner.jdbc.StatementResultSetHandler. Invokes the IntrospectingResultSetFactory to execute Statement objects.
- IntrospectingPreparedStatementResultSetHandler extends com.mockrunner.jdbc.PreparedStatementResultSetHandler. Invokes the IntrospectingResultSetFactory to execute PreparedStatement objects.
- IntrospectingCallableStatementResultSetHandler extends com.mockrunner.jdbc.CallableStatementResultSetHandler. Invokes the IntrospectingResultSetFactory to execute CallableStatement objects.
- IntrospectingResultSetHandler implements the com.mockrunner.jdbc.ResultSetHandler. Implements multi-resultset handling and getting the result sets using Introspection.
A sample test file which uses the SQLUnit mock testing framework can be found at test/mock/test.xml in the distribution.
|
Java Source File Name | Type | Comment |
AbstractMockDatabase.java | Class | Abstract base class from which all IMockDatabase objects must extend. |
ColumnMetaData.java | Class | Container for Column Metadata information. |
IMockDatabase.java | Interface | Provides a convenient abstraction of what a mock database should look
like for SQLUnit mock testing. |
IntrospectingCallableStatementResultSetHandler.java | Class | Extends the CallableStatementResultSetHandler to use introspection. |
IntrospectingPreparedStatementResultSetHandler.java | Class | Extends the PreparedStatementResultSetHandler to use introspection. |
IntrospectingResultSetFactory.java | Class | Introspects a given IMockDataStore object and invokes the named method
to return ResultSet objects. |
IntrospectingStatementResultSetHandler.java | Class | Extends the StatementResultSetHandler to use introspection. |
MockInitialContext.java | Class | Overrides the InitialContext to provide an in-memory lookup mechanism
instead of network lookup. |
MockInitialContextFactory.java | Class | Builds a MockInitialContext using an environment Hashtable. |
MockResultSetUtils.java | Class | Collection of utility methods to manipulate MockResultSets. |
SQLUnitMockCallableStatement.java | Class | Overrides certain methods in the MockCallableStatement class for mock testing
of SQLUnit. |
SQLUnitMockConnection.java | Class | Represents a Connection object that depends on an underlying IDatabase. |
SQLUnitMockDatabase.java | Class | Mock database to supply results. |
SQLUnitMockDriver.java | Class | Implements a Mock JDBC driver for SQLUnit. |
SQLUnitMockPreparedStatement.java | Class | Overrides certain methods in the MockPreparedStatement class for mock
testing of SQLUnit. |
SQLUnitMockStatement.java | Class | Overrides certain methods in the MockStatement class for mock testing
of SQLUnit. |
VarSetter.java | Class | Simple class whose method is called from the set tag. |