ArrayResultSetFactory.java | Class | A ResultSetFactory implementation which will produce
MockResultSet instances based on information given as
String arrays.
StringValuesTable and ArrayResultSetFactory can
provide easy set up of unit test fixtures and assertion of outcomes with the
same data structures, without any need for external sources of test data:
private static final String _SQL_SELECT_ALL_EMPLOYEES =
"SELECT * FROM employee";
private StringValuesTable _employeeQueryResults;
ArrayResultSetFactory _arrayResultSetFactory;
private Employee[] _employees;
protected void setUp() throws Exception {
super.setUp();
_employeeQueryResults = new StringValuesTable(
"employeeQueryResults",
new String[] {
"id", "lastname", "firstname",
},
new String[][] {
new String[] {"1", "gibbons", "peter"},
new String[] {"2", "lumbergh", "bill"},
new String[] {"3", "waddams", "milton"},
}
);
_employees = new Employee[3] {
new Employee(
_employeeQueryResults.getItem(1, "id"),
_employeeQueryResults.getItem(1, "lastname"),
_employeeQueryResults.getItem(1, "firstname"),
),
...
};
...
}
public void testGetEmployees() throws Exception {
PreparedStatementResultSetHandler preparedStatementResultSetHandler =
getPreparedStatementResultSetHandler();
_arrayResultSetFactory =
new ArrayResultSetFactory(_employeeQueryResults);
MockResultSet resultSet =
preparedStatementResultSetHandler.createResultSet(
_employeeQueryResults.getName(),
arrayResultSetFactory);
preparedStatementResultSetHandler.prepareResultSet(
_SQL_SELECT_ALL_EMPLOYEES, resultSet);
// execute query, perhaps calling method on an EmployeeDAO...
assertEquals(
_employeeQueryResults.getNumberOfRows(),
resultsList.size());
for (int i = 0; i < _employees.length; i++) {
assertTrue(resultsList.contains(_employees[i]));
}
MockResultSet mockResultSet =
preparedStatementResultSetHandler.getResultSet(
SQL_SELECT_ALL_EMPLOYEES);
int rows = mockResultSet.getRowCount();
for (int row = 1; row <= rows; row++) {
verifyResultSetRow(
_employeeQueryResults.getName(),
row, _employeeQueryResults.getRow(row));
}
verifySQLStatementExecuted(_SQL_SELECT_ALL_EMPLOYEES);
verifyAllResultSetsClosed();
verifyAllStatementsClosed();
verifyConnectionClosed();
}
author: Erick G. |