0

I try to set up a test with mockrunner for the first time. The connection is established, but I only get an empty result set. Here is the condensed working example:

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()); // []
    }
}

The expected output is a resultset with "1" and "foobar", as well as the SQL statement that is executed.

phobic
  • 914
  • 10
  • 24
  • If I pass the sql statement to `executeQuery()` it works as expected. Though, for a PreparedStatement this should not be necessary. Normal statements work as expected, too. – phobic Feb 23 '15 at 17:17
  • There are methods specific to PreparedStatement objects: `getJDBCMockObjectFactory().getMockConnection().getPreparedStatementResultSetHandler()` and `getPreparedStatements()`. But I still get an empty result set, even though I use them. – phobic Feb 24 '15 at 07:36

2 Answers2

2

You need to use the PreparedStatementResultSetHandler.

Here is a working modified version of your test:

@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");
}
Domenic D.
  • 5,276
  • 4
  • 30
  • 41
  • As you can see above, I have already answered the question, and tried your solution, which did not yet work. I contacted the authors of mockrunner and the issue was resolved by the developers several month later: https://github.com/mockrunner/mockrunner/issues/11 Thanks to you I noticed the update and will therefore accept your answer, as it also shows the preferred solution. – phobic Jan 18 '16 at 13:12
0

By exploring the API, I came across methods that are specifically meant to be used with PreparedStatements instances that I was not aware about:

getJDBCMockObjectFactory().getMockConnection().getPreparedStatementResultSetHan‌​dler() and getPreparedStatements(). Even though I was not able to retrieve the mocked result set. statementHandler.prepareGlobalResultSet(resultMock); does not seem to add the mocked result set. This might be a bug in mockrunner, so I will contact the author(s) about it. A workaround is to use statementHandler.prepareResultSet("SELECT * FROM", resultMock, new HashMap());. Here is a complete working example, in case you experience the same error:

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());
    }
}
phobic
  • 914
  • 10
  • 24