How to stub/mock JDBC ResultSet to work both with Java 5 and 6?

人走茶凉 提交于 2019-11-30 04:37:29

问题


I'm testing some of my classes working with JDBC statements etc and now I got problem with JDBC ResultSet interface:

The software should run both with Java 5 and Java 6 and hence the tests should also be run with both versions. Unfortunately Java 6 has introduced a bunch of new methods (which is still not a big deal) that return a bunch of new classes/interfaces, which makes the things more difficult. (see Frank Carver’s Punch Barrel - Java 6 breaks JDBC for example)

Before finding out these version differences, I considered between stubbing and mocking and ended up with stubbing because the ResultSet has internal state (current row handled) that is more natural to me to work with stubs, as :

public class StubResultSet implements ResultSet {
    private Object[][] data;
    private int currentRow = -1;
    private boolean closed = false;

    public StubResultSet(Object[][] data) {
        this.data = data;
    }

    public Object getObject(int columnIndex) throws SQLException {
        checkClosed();
        return data[currentRow][columnIndex];
    }
    public String getString(int columnIndex) throws SQLException {
        checkClosed();
        return (String) getObject(columnIndex);
    }

    // ...
}

But if I don't introduce the new methods as public NClob getNClob(int columnIndex), the class is broken under Java 6 - if I introduce them the class in broken under Java 5.

I can work with mockito (for example) callbacks to have the state being reflected with the return values but does somebody have some other - maybe more nice - idea?


回答1:


Well, after some thinking I ended up having the stub class as there and mocking it with Mockito as:

public static ResultSet initMock(Object[][] data) throws SQLException {
    final StubResultSetContents contents = new StubResultSetContents(data);
    ResultSet rs = mock(ResultSet.class, RETURNS_SMART_NULLS);
    when(rs.getObject(anyInt())).thenAnswer(new Answer<Object>() {
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return contents.getObject(getIntArgument(invocation));
        }
    });
    // a bunch of similar when(...).thenAnswer(...) constructs...
}

(stub class in StubResultSetContents). If somebody has some other ideas, feel free to answer =)




回答2:


I had the same problem and solved it using a Proxy implementation. It seems like it's working pretty good.

public class TestResultSet implements InvocationHandler {
    public static ResultSet createProxy(HashMap<Object, Object>[] rows) {
        return (ResultSet) Proxy.newProxyInstance(ResultSet.class.getClassLoader(),
                                             new Class[] { ResultSet.class },
                                             new TestResultSet(rows));
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // Find the equivalent method in the proxy class.
        Method m = TestResultSet.class.getMethod(method.getName(), method.getParameterTypes());
        if(m == null) {
            throw new SQLException("Unsupported method " + method.getName());
        }

        return m.invoke(this, args);
    }

    // Method implementations follow here (only one supplied as an example)

    public boolean isFirst() throws SQLException {
        return index ==0;
    }

}


来源:https://stackoverflow.com/questions/919544/how-to-stub-mock-jdbc-resultset-to-work-both-with-java-5-and-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!