<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=nealeu" rel="nofollow">Neale Upstone</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-8503?redirect=false" rel="nofollow">SPR-8503</a></strong> and commented</p>
<p dir="auto">When using <code class="notranslate">@ContextConfiguration</code> in a test <code class="notranslate">@Autowired</code> annotated fields get wired, even if context:annotation-driven/ has not been specified.</p>
<p dir="auto">This is incorrect behaviour, as it means that a test can pass against a context configuration which when used in production will not behave that way.</p>
<p dir="auto">A better approach would be to load the specified configuration into one application context, and then use a child context for wiring the test fixture.</p>
<p dir="auto">The child context can then have the additional post processors, without polluting the application context under test.</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.1 M2</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="398086790" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/9309" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/9309/hovercard" href="https://github.com/spring-projects/spring-framework/issues/9309">#9309</a> Load dedicated child ApplicationContext for test instance in the TestContext framework (<em><strong>"duplicates"</strong></em>)</li>
</ul>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=eberhardwolff" rel="nofollow">Eberhard Wolff</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-4632?redirect=false" rel="nofollow">SPR-4632</a></strong> and commented</p>
<h4 dir="auto">Status Quo</h4>
<p dir="auto">The <code class="notranslate">loadContext(\*\)</code> methods in <code class="notranslate">AbstractGenericContextLoader</code> and <code class="notranslate">AbstractGenericWebContextLoader</code> invoke <code class="notranslate">AnnotationConfigUtils.registerAnnotationConfigProcessors(context)</code>.</p>
<p dir="auto">The result is that <code class="notranslate">@Autowired</code>, <code class="notranslate">@Resource</code>, <code class="notranslate">@Inject</code>, etc. all work by default, even if annotation-driven dependency injection is not explicitly configured in the test's <code class="notranslate">ApplicationContext</code>. Consequently, if a developer is not aware of this, then errors related to the lack of explicit annotation-driven DI support in production configuration will not be noticed until after testing which is typically undesirable.</p>
<p dir="auto">Note, however, that this unexpected behavior only applies to XML-based configuration. With JavaConfig, an <code class="notranslate">AnnotatedBeanDefinitionReader</code> (used internally by <code class="notranslate">AnnotationConfigContextLoader</code> and <code class="notranslate">AnnotationConfigWebContextLoader</code>) automatically registers the <em>annotation config processors</em>.</p>
<p dir="auto">Furthermore, <code class="notranslate">BeanPostProcessors</code> may inadvertently be applied to the test instance -- even though the test is not truly a <em>bean</em> in the <code class="notranslate">ApplicationContext</code>. This leads to potentially problematic behavior such as accidental proxying of the test instance, as described in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398151000" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14113" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14113/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14113">#14113</a>.</p>
<hr>
<h4 dir="auto">Deliverables</h4>
<ol class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> As suggested in comments for this issue, create an <code class="notranslate">ApplicationContext</code> dedicated solely to the test instance (for the purpose of dependency injection and bean initialization) and set the <code class="notranslate">parent</code> of that context to the <code class="notranslate">ApplicationContext</code> loaded by the TCF.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> Stop invoking <code class="notranslate">AnnotationConfigUtils.registerAnnotationConfigProcessors()</code> in:
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> <code class="notranslate">AbstractGenericContextLoader</code></li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> <code class="notranslate">AbstractGenericWebContextLoader</code></li>
</ul>
</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> Ensure that <em>child</em> contexts are properly closed with regard to context caching in the TCF.</li>
</ol>
<hr>
<h4 dir="auto">Original Issue Summary</h4>
<p dir="auto"><em>Do not enable annotation-driven DI for the entire ApplicationContext in the TestContext framework</em></p>
<h4 dir="auto">Original Proposal</h4>
<p dir="auto">It would be better if the TestContext framework (TCF) only enabled annotation-driven dependency injection for test instances and not for the entire <code class="notranslate">ApplicationContext</code>.</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 2.5.2</p>
<p dir="auto"><strong>Attachments:</strong></p>
<ul dir="auto">
<li><a href="https://jira.spring.io/secure/attachment/14662/AnnotationConfigProcessorTestExecutionListener.java" rel="nofollow">AnnotationConfigProcessorTestExecutionListener.java</a> (<em>1.42 kB</em>)</li>
</ul>
<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="398113354" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13149" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13149/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13149">#13149</a> Spring Test should not modify the application context under test (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398153893" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14496" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14496/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14496">#14496</a> Support WebApplicationContext hierarchies in the TestContext Framework</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398094026" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/10284" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/10284/hovercard" href="https://github.com/spring-projects/spring-framework/issues/10284">#10284</a> Provide support for context hierarchies in the TestContext Framework</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398187049" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/18295" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/18295/hovercard" href="https://github.com/spring-projects/spring-framework/issues/18295">#18295</a> Support 'throw-away' context for a test class</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398097137" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/10719" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/10719/hovercard" href="https://github.com/spring-projects/spring-framework/issues/10719">#10719</a> Provide mechanism for disabling automatic annotation-driven autowiring in tests</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398151000" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14113" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14113/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14113">#14113</a> Test instances should not be proxied in the TestContext framework</li>
</ul>
<p dir="auto">6 votes, 6 watchers</p>