0

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

4

2 回答 2

2

您需要使用 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");
}
于 2016-01-13T12:55:31.227 回答
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());
    }
}
于 2015-02-24T08:39:32.720 回答