<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=specialagent" rel="nofollow">Martin Dames</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-4454?redirect=false" rel="nofollow">SPR-4454</a></strong> and commented</p>
<p dir="auto">Hey,</p>
<p dir="auto">I used the normal JdbcDaoSupport class for my DAO's, and I mocked the JdbcTemplate within my test classes by using the .setJdbcTemplate() method. So, now I want to use the SimpleJdbcTemplate in my DAO and I inherited my DAO from the org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport.<br>
I wanted to mock the SimpleJdbcTemplate as well but I couldn't find any obvious Spring way to use a Mock in this situation.</p>
<p dir="auto">I don't know if this isn't wanted by you guys, but I tried a patch and the tests are running fine:</p>
<p dir="auto">I wrote my own SimpleJdbcDaoSupport class, which is the same as the normal one, but offers a Setter method for the SimpleJdbcTemplate.</p>
<p dir="auto">If this sounds quite reasonable for you, just commit the patch to your next Spring version as you like.</p>
<p dir="auto">/**<br>
*</p>
<ul dir="auto">
<li>
<p dir="auto"><code class="notranslate">@author</code> Martin Dames<br>
*/<br>
public class MyOwnSimpleJdbcDaoSupport extends JdbcDaoSupport {</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="private SimpleJdbcTemplate simpleJdbcTemplate;"><pre class="notranslate"><code class="notranslate">private SimpleJdbcTemplate simpleJdbcTemplate;
</code></pre></div>
<p dir="auto">/**</p>
<ul dir="auto">
<li>Create a SimpleJdbcTemplate based on the configured JdbcTemplate.<br>
*/<br>
<code class="notranslate">@Override</code><br>
protected void initTemplateConfig() {<br>
this.simpleJdbcTemplate = new SimpleJdbcTemplate(getJdbcTemplate());<br>
}</li>
</ul>
<p dir="auto">/**</p>
<ul dir="auto">
<li>
<p dir="auto">Return a SimpleJdbcTemplate wrapping the configured JdbcTemplate.<br>
*/<br>
public SimpleJdbcTemplate getSimpleJdbcTemplate() {<br>
return this.simpleJdbcTemplate;<br>
}</p>
<p dir="auto">/**</p>
</li>
<li>
<p dir="auto">Sets an other SimpleJdbcTemplate, can be used for mocking the template.<br>
*/<br>
public void setSimpleJdbcTemplate(SimpleJdbcTemplate simpleJdbcTemplate) {<br>
this.simpleJdbcTemplate = simpleJdbcTemplate;<br>
}<br>
}</p>
</li>
</ul>
</li>
</ul>
<hr>
<p dir="auto"><strong>Affects:</strong> 2.5.1</p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398085533" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/9133" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/9133/hovercard" href="https://github.com/spring-projects/spring-framework/issues/9133">#9133</a> Ability to change the SimpleJdbcTemplate in DAO's (<em><strong>"is duplicated by"</strong></em>)</li>
</ul>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=andrei.badea" rel="nofollow">Andrei Badea</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-7824?redirect=false" rel="nofollow">SPR-7824</a></strong> and commented</p>
<p dir="auto">Have the following test classes:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@ContextConfiguration(...)
public class MyTestBase extends AbstractTestNGSpringContextTests {

	protected boolean beforeTransaction;

	@Test
	public void test() {
		Assert.assertTrue(beforeTransaction);
	}
}"><pre class="notranslate"><code class="notranslate">@ContextConfiguration(...)
public class MyTestBase extends AbstractTestNGSpringContextTests {

	protected boolean beforeTransaction;

	@Test
	public void test() {
		Assert.assertTrue(beforeTransaction);
	}
}
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@TestExecutionListeners({ TransactionalTestExecutionListener.class })
@Transactional
public class MyTest extends MyTestBase {

	@BeforeTransaction
	public void before() {
		beforeTransaction = true;
	}
}"><pre class="notranslate"><code class="notranslate">@TestExecutionListeners({ TransactionalTestExecutionListener.class })
@Transactional
public class MyTest extends MyTestBase {

	@BeforeTransaction
	public void before() {
		beforeTransaction = true;
	}
}
</code></pre></div>
<p dir="auto">Run MyTest.java. The expectation is that the <code class="notranslate">@Transactional</code> annotation is handled and the test() method runs in a transaction. This does not happen. AbstractFallbackTransactionAttributeSource seems to have been written with interfaces in mind, where  the "most specific method" is in the implementation class. So for the case above, one would expect the most specific method to be MyTest.test(), but it actually is MyTestBase.test(). I did not find a way to get a java.lang.reflect.Method handle for MyTest.test().</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.0.4</p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398164036" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/15877" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/15877/hovercard" href="https://github.com/spring-projects/spring-framework/issues/15877">#15877</a> <code class="notranslate">@Transactional</code> annotation does not apply to inherited methods from abstract class during tests (<em><strong>"is duplicated by"</strong></em>)</li>
</ul>