title: How to stub/mock JDBC ResultSet to work both with Java 5 and 6?
body: <p>I'm testing some of my classes working with JDBC statements etc and now I got problem with JDBC ResultSet interface:</p>
  
  <p>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 <a href="http://blog.punchbarrel.com/2008/09/23/java-6-breaks-jdbc/" rel="noreferrer">Frank Carver’s Punch Barrel - Java 6 breaks JDBC</a> for example)</p>
  
  <p>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 :</p>
  
  <pre><code>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);
      }
  
      // ...
  }
  </code></pre>
  
  <p>But if I don't introduce the new methods as <code>public NClob getNClob(int columnIndex)</code>, the class is broken under Java 6 - if I introduce them the class in broken under Java 5.</p>
  
  <p>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?</p>