mockrunner jdbc - 结果集中没有 columns/rows
mockrunner jdbc - no columns/rows in result set
我第一次尝试使用 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 语句。
通过探索 API,我发现了专门用于我不知道的 PreparedStatements 实例的方法:
getJDBCMockObjectFactory().getMockConnection().getPreparedStatementResultSetHandler()
和 getPreparedStatements()
。即使我无法检索模拟的结果集。
statementHandler.prepareGlobalResultSet(resultMock);
似乎没有添加模拟结果集。这可能是 mockrunner 中的一个错误,所以我会就此联系作者。解决方法是使用 statementHandler.prepareResultSet("SELECT * FROM", resultMock, new HashMap());
。这是一个完整的工作示例,以防您遇到相同的错误:
import java.sql.*;
import java.util.HashMap;
import org.junit.Test;
import com.mockrunner.jdbc.BasicJDBCTestCaseAdapter;
import com.mockrunner.jdbc.PreparedStatementResultSetHandler;
import com.mockrunner.mock.jdbc.MockResultSet;
public class ExtractDataTest extends BasicJDBCTestCaseAdapter {
@Test
public void test() throws Exception {
PreparedStatementResultSetHandler statementHandler = getJDBCMockObjectFactory()
.getMockConnection().getPreparedStatementResultSetHandler();
MockResultSet resultMock = statementHandler.createResultSet();
resultMock.addColumn("ID", new Object[]{"1"});
resultMock.addColumn("USERNAME", new Object[]{"foobar"});
statementHandler.prepareResultSet("SELECT * FROM DUAL", resultMock, new HashMap());
Connection con = DriverManager.getConnection( "a", "b", "c");
System.out.println(con); //com.mockrunner.mock.jdbc.MockConnection@29d8a2c5
PreparedStatement stmtObjects = con.prepareStatement(
"SELECT * FROM DUAL WHERE 1=?"); //SELECT * FROM DUAL would work, too.
ResultSet rs = stmtObjects.executeQuery();
System.out.println(rs);
System.out.println(this.getPreparedStatements());
}
}
您需要使用 PreparedStatementResultSetHandler。
这是您测试的有效修改版本:
@Test
public void test() throws Exception {
PreparedStatementResultSetHandler statementHandler = getJDBCMockObjectFactory()
.getMockConnection().getPreparedStatementResultSetHandler();
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");
PreparedStatement stmtObjects = con.prepareStatement(
"SELECT * FROM USER");
ResultSet rs = stmtObjects.executeQuery();
assertTrue(rs.next());
//System.out.println(rs);
//System.out.println(getExecutedSQLStatements());
verifySQLStatementExecuted("SELECT * FROM USER");
}
我第一次尝试使用 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 语句。
通过探索 API,我发现了专门用于我不知道的 PreparedStatements 实例的方法:
getJDBCMockObjectFactory().getMockConnection().getPreparedStatementResultSetHandler()
和 getPreparedStatements()
。即使我无法检索模拟的结果集。
statementHandler.prepareGlobalResultSet(resultMock);
似乎没有添加模拟结果集。这可能是 mockrunner 中的一个错误,所以我会就此联系作者。解决方法是使用 statementHandler.prepareResultSet("SELECT * FROM", resultMock, new HashMap());
。这是一个完整的工作示例,以防您遇到相同的错误:
import java.sql.*;
import java.util.HashMap;
import org.junit.Test;
import com.mockrunner.jdbc.BasicJDBCTestCaseAdapter;
import com.mockrunner.jdbc.PreparedStatementResultSetHandler;
import com.mockrunner.mock.jdbc.MockResultSet;
public class ExtractDataTest extends BasicJDBCTestCaseAdapter {
@Test
public void test() throws Exception {
PreparedStatementResultSetHandler statementHandler = getJDBCMockObjectFactory()
.getMockConnection().getPreparedStatementResultSetHandler();
MockResultSet resultMock = statementHandler.createResultSet();
resultMock.addColumn("ID", new Object[]{"1"});
resultMock.addColumn("USERNAME", new Object[]{"foobar"});
statementHandler.prepareResultSet("SELECT * FROM DUAL", resultMock, new HashMap());
Connection con = DriverManager.getConnection( "a", "b", "c");
System.out.println(con); //com.mockrunner.mock.jdbc.MockConnection@29d8a2c5
PreparedStatement stmtObjects = con.prepareStatement(
"SELECT * FROM DUAL WHERE 1=?"); //SELECT * FROM DUAL would work, too.
ResultSet rs = stmtObjects.executeQuery();
System.out.println(rs);
System.out.println(this.getPreparedStatements());
}
}
您需要使用 PreparedStatementResultSetHandler。
这是您测试的有效修改版本:
@Test
public void test() throws Exception {
PreparedStatementResultSetHandler statementHandler = getJDBCMockObjectFactory()
.getMockConnection().getPreparedStatementResultSetHandler();
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");
PreparedStatement stmtObjects = con.prepareStatement(
"SELECT * FROM USER");
ResultSet rs = stmtObjects.executeQuery();
assertTrue(rs.next());
//System.out.println(rs);
//System.out.println(getExecutedSQLStatements());
verifySQLStatementExecuted("SELECT * FROM USER");
}