title: using JMock to write unit test for a simple spring JDBC DAO
body: <p>I'm writing an unit test for spring jdbc dao. The method to test is:</p>
  
  <pre><code>public long getALong() {
  return simpleJdbcTemplate.queryForObject("sql query here", new RowMapper&lt;Long&gt;() {
    public Long mapRow(ResultSet resultSet, int i) throws SQLException {
      return resultSet.getLong("a_long");
    }
  });
  </code></pre>
  
  <p>}</p>
  
  <p>Here is what I have in the test:</p>
  
  <pre><code>public void testGetALong() throws Exception {
      final Long result = 1000L;
      context.checking(new Expectations() {{
        oneOf(simpleJdbcTemplate).queryForObject("sql_query", new RowMapper&lt;Long&gt;() {
          public Long mapRow(ResultSet resultSet, int i) throws SQLException {
            return resultSet.getLong("a_long");
          }
        });
        will(returnValue(result));
      }});
      Long seq = dao.getALong();
      context.assertIsSatisfied();
      assertEquals(seq, result);
    }
  </code></pre>
  
  <p>Naturally, the test doesn't work (otherwise, I wouldn't be asking this question here). The problem is the rowmapper in the test is different from the rowmapper in the DAO. So the expectation is not met. </p>
  
  <p>I tried to put <code>with</code> around the sql query and <code>with(any(RowMapper.class))</code> for the rowmapper. It wouldn't work either, complains about "not all parameters were given explicit matchers: either all parameters must be specified by matchers or all must be specified by values, you cannot mix matchers and values"</p>