mockrunner jdbc - 结果集中没有列/行

时间:2015-02-23 14:33:15

标签: java unit-testing jdbc mockrunner

我尝试首次使用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语句。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

通过探索API,我遇到了一些特别适用于我不了解的PreparedStatements实例的方法:

getJDBCMockObjectFactory().getMockConnection().getPreparedStatementResultSetHan‌​dler()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());
    }
}