我第一次尝试使用 mockrunner 进行测试。连接已建立,但我只得到一个空的结果集。这是精简的工作示例:
import com.mockrunner.jdbc.BasicJDBCTestCaseAdapter;
import com.mockrunner.jdbc.StatementResultSetHandler;
import com.mockrunner.mock.jdbc.MockResultSet;
public class ExtractDataTest extends BasicJDBCTestCaseAdapter {
@Test
public void test() throws Exception {
StatementResultSetHandler statementHandler = getJDBCMockObjectFactory()
.getMockConnection().getStatementResultSetHandler();
MockResultSet resultMock = statementHandler.createResultSet();
resultMock.addColumn("ID", new Object[]{"1"});
resultMock.addColumn("USERNAME", new Object[]{"foobar"});
statementHandler.prepareGlobalResultSet(resultMock);
Connection con = DriverManager.getConnection( "a", "b", "c");
System.out.println(con); //com.mockrunner.mock.jdbc.MockConnection@29d8a2c5
PreparedStatement stmtObjects = con.prepareStatement(
"SELECT * FROM USER");
ResultSet rs = stmtObjects.executeQuery();
System.out.println(rs); // empty result set
System.out.println(this.getExecutedSQLStatements()); // []
}
}
预期的输出是带有“1”和“foobar”的结果集,以及执行的 SQL 语句。