模拟运行器 JDBC - 结果集中没有列/行



我第一次尝试用模拟跑步者设置测试。连接已建立,但我只得到一个空的结果集。这是精简的工作示例:

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 语句。

您需要使用 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");
}

通过探索 API,我遇到了专门用于我不知道的 PreparedStatements 实例的方法:

getJDBCMockObjectFactory().getMockConnection().getPreparedStatementResultSetHan‌​dler()getPreparedStatements().即使我无法检索模拟的结果集。 statementHandler.prepareGlobalResultSet(resultMock);似乎没有添加模拟结果集。这可能是模拟运行器中的一个错误,所以我会联系作者。解决方法是使用 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());
    }
}

相关内容

  • 没有找到相关文章

最新更新