title: Using Spock to test Spring JdbcTemplate
body: <p>I have the following Java 8 DAO method that uses Spring JdbcTemplate to hit a database:</p>
  
  <pre><code>class MyAppDao {
      @Inject
      DataSource dataSource
  
      public List&lt;String&gt; getFizzByBuzzIds(List&lt;Integer&gt; buzzIds) {
          String sql = "SELECT * from buzzes WHERE buzz_id IN ( :buzzIds )";
  
          NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
          Map&lt;String, Object&gt; paramValues = new HashMap&lt;&gt;(1);
          paramValues.put("buzzIds", buzzIds);
  
          List&lt;Buzz&gt; buzzes = jdbcTemplate.query(sql,
              paramValues, (rs, rowNum) -&gt; {
                  return new Buzz(rs.getString("buzz_id"), rs.getString("buzz_key"),
                  rs.getString("buzz_external_id"));
              }
          );
  
          List&lt;String&gt; fizzes = figureOutSomehow();
  
          return fizzes;
      }
  }
  </code></pre>
  
  <p>I <strong>cannot</strong> change the dependencies injected into this method. The <code>NamedParameterJdbcTemplate</code> instance <em>must</em> be defined inside this method. The only thing I can mock (easily) is the <code>dataSource</code>.</p>
  
  <p>I am <em>trying</em> (and I <strong>know</strong>, the method isn't written to be conducive to unit tests at all!) to write a Spock unit test for this, and just want to verify that when <code>getFizzByBuzzIds</code> is called, that <code>jdbcTemplate.query</code> is called (with <em>any</em> arguments).</p>
  
  <p>My best attempt:</p>
  
  <pre><code>def 'getFizzByBuzzIds attempts to get case info from the DB'() {
      given:
      MyAppDao dao = Spy(MyAppDao, constructorArgs: [ dataSource ]) {
          getJdbcTemplate() &gt;&gt; Mock(JdbcTemplate) {
              1 * query('', null)   // ???
          }
      }
  
      when:
      List&lt;Integer&gt; buzzIds = [1, 2, 3, 4]
      dao.getFizzByBuzzIds(buzzIds)
  
      then:
      1 * dao.jdbcTemplate.query('', null)    // ???
  }
  </code></pre>
  
  <p>How can I configure the <code>Spy</code> and the <code>then</code> block label so that executing <code>getFizzByBuzzIds</code> verifies whether the underlying <code>JdbcTemplate.query(...)</code> method is called?</p>