title: Why is mockito's when().thenReturn() not working?
body: <p>I am testing <code>JdbcSpitterDao#getSpitterByid()</code> and am mocking <code>jdbcTemplate.queryForObject()</code></p>
  
  <pre><code>public class JdbcSpitterDao extends JdbcDaoSupport implements SpitterDao {
  ...
      public Spitter getSpitterById(long id,
              ParameterizedRowMapper&lt;Spitter&gt; parameterizedRowMapper) {
          JdbcTemplate jdbcTemplate = getJdbcTemplate();
          return jdbcTemplate.queryForObject(SQL_SELECT_SPITTER,
                  parameterizedRowMapper, id);
      }
  ...
  }
  </code></pre>
  
  <p>My test object contains the following</p>
  
  <pre><code>    ParameterizedRowMapper&lt;Spitter&gt; parameterizedRowMapper = new ParameterizedRowMapper&lt;Spitter&gt;() {
          public Spitter mapRow(ResultSet rs, int rowNum) throws SQLException {
              Spitter Spitter = new Spitter();
              spitter.setId(rs.getLong(1));
              spitter.setUsername(rs.getString(2));
              spitter.setPassword(rs.getString(3));
              spitter.setFullName(rs.getString(4));
              spitter.setEmail(rs.getString(5));
              return spitter;
          }
      };
      JdbcTemplate jdbcTemplate = mock(JdbcTemplate.class);
      JdbcSpitterDao jdbcSpitterDao = new JdbcSpitterDao();   
      jdbcSpitterDao.setJdbcTemplate(jdbcTemplate);
  
      // Don't connect to the database.  Mock JdbcTemplate class
      when(
              jdbcTemplate.queryForObject(
                      JdbcSpitterDao.SQL_SELECT_SPITTER,
                      parameterizedRowMapper, 1)).thenReturn(new Spitter(1, "rajkumarm", "rajmukarm",
                              "Rajkumar Masaniayan", "rajkumarm@gmail.com"));
  
      // Actual test
      Spitter actualSpitter = jdbcSpitterDao.getSpitterById(1, parameterizedRowMapper);
  </code></pre>
  
  <p>But <code>when().thenReturn()</code> idiom is not getting invoked.  The control is flowing to actual <code>jdbcTemplate</code>.  Looks like there is a problem with</p>
  
  <pre><code>JdbcTemplate jdbcTemplate = mock(JdbcTemplate.class);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>jdbcSpitterDao.setJdbcTemplate(jdbcTemplate);
  </code></pre>
  
  <p>Would you please let me know what is going wrong?</p>