title: Spring JUnit and Mockito - SimpleJdbcTemplate
body: <p>Given a class that extends SimpleJdbcDaoSupport, how can you mock SimpleJdbcTemplate?</p>
  
  <pre><code>public class SimpleJdbcDaoSupportExtension extends SimpleJdbcDaoSupport {  
       public SimpleJdbcDaoSupportExtension (JdbcTemplate jdbcTemplate){  
               super.setJdbcTemplate(jdbcTemplate);  
       }
  
       public MyDomainObj getResult(){
           SimpleJdbcTemplate sjdbc = getSimpleJdbcTemplate();  
           MyDomainObj result = sjdbc.query(*whatever necessary args*.);
           return result;
       }
  }
  </code></pre>
  
  <p>Then, using Mockito:  </p>
  
  <pre><code>public class Test {  
      @Mock private JdbcTemplate mockedJdbcTemplateDedendency;  
      private SimpleJdbcDaoSupportExtension testObj;  
  
      @Before
      public void doBeforeEachTestCase() {
          MockitoAnnotations.initMocks(this);
          SimpleJdbcDaoSupportExtension sje = new SimpleJdbcDaoSupportExtension (mockedJdbcTemplateDedendency);
      }  
      @Test
      public final void test(){           
          when(mockedJdbcTemplateDedendency.query("what to query").thenReturn(new MyDomainObj());
      }
  }
  </code></pre>
  
  <p>The mocked JdbcTemplate is injected, but since the dao class relies on SimpleJdbcTemplate to make queries (for mapping to objects), and it's constructed internally by SimpleJdbcDaoSupport - mocking JdcbTemplate has no effect on the SimpleJdbcTemplate. So how to do this, when there are no public setters for it, and the only way to construst SimpleJdbcTemplate is to rely on that method, getSimpleJdbcObject()?</p>