Spocking the JDBC
<p>I have some Groovy 2.4.x code that uses some JDBC:</p>

<pre><code>class WidgetPersistor {
    @Inject // Gets injected correctly by Guice, don't worry about it!
    DataSource dataSource

    Fizz getFizzByWidgetName(String name) {
        Connection conn
        PreparedStatement ps
        ResultSet rs

        try {
          // JDBC code here
        } catch(SQLException sqlExc) {
            if(conn) {
                try {
                    // NOTE: At the end of the day, I just want to verify
                    // that, given the 'name' arg to this method, the rollback
                    // doesn't fire!
                    conn.rollback()
                } catch(SQLException rollBackExc) {
                    throw rollBackExc
                }
            }
            throw sqlExc
        } finally {
            if(conn) {
                try {
                    rs.close()
                    ps.close()
                    conn.close()
                } catch(SQLException closingExc) {
                    throw closingExc
                }
            }
        }   
    }
}
</code></pre>

<p>I am trying to write a Spock test that will execute this <code>getFizzByWidgetName</code> method and verify that the <code>conn.rollback()</code> method <em>never</em> executed (meaning we never tried to rollback).</p>

<p>Here's my best attempt:</p>

<pre><code>def "getFizzByWidgetName succeeds without rollback"() {
  given: "data client with db connections"
    // Don't worry about how I get this for my test, but its a legit JDBC connection
    DataSource ds = provideDataSource()

    Connection mockConn = Mock(Connection)
    PreparedStatement mockPS = Mock(PreparedStatement)
    ResultSet mockRS = Mock(ResultSet)

    mockPS.executeQuery() &gt;&gt; mockRS
    mockConn.prepareStatement(_) &gt;&gt; mockPS
    ds.connection &gt;&gt; mockConn   // ??? Its like I want the DataSource half-mocked...

    WidgetPersistor client = new WidgetPersistor(mockDS)

    when: "we try to query something"
    client.getFizzByWidgetName('fizzbuzz')

    then: "we dont get any errors"
    0 * mockConn.rollback()
}
</code></pre>

<p>Any ideas where I'm going awry?</p>