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

前端 未结 2 615
孤城傲影
孤城傲影 2021-01-05 07:26

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 Ja

2条回答
  •  耶瑟儿~
    2021-01-05 07:45

    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() {
            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 =)

    提交回复
    热议问题