Issue 1:
Neale Upstone opened SPR-8503 and commented

When using @ContextConfiguration in a test @Autowired annotated fields get wired, even if context:annotation-driven/ has not been specified.

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.

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.

The child context can then have the additional post processors, without polluting the application context under test.

Affects: 3.1 M2

Issue Links:

#9309 Load dedicated child ApplicationContext for test instance in the TestContext framework ("duplicates")

Issue 2:
Eberhard Wolff opened SPR-4632 and commented

Status Quo

The loadContext(\*\) methods in AbstractGenericContextLoader and AbstractGenericWebContextLoader invoke AnnotationConfigUtils.registerAnnotationConfigProcessors(context).

The result is that @Autowired, @Resource, @Inject, etc. all work by default, even if annotation-driven dependency injection is not explicitly configured in the test's ApplicationContext. 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.

Note, however, that this unexpected behavior only applies to XML-based configuration. With JavaConfig, an AnnotatedBeanDefinitionReader (used internally by AnnotationConfigContextLoader and AnnotationConfigWebContextLoader) automatically registers the annotation config processors.

Furthermore, BeanPostProcessors may inadvertently be applied to the test instance -- even though the test is not truly a bean in the ApplicationContext. This leads to potentially problematic behavior such as accidental proxying of the test instance, as described in #14113.

Deliverables

As suggested in comments for this issue, create an ApplicationContext dedicated solely to the test instance (for the purpose of dependency injection and bean initialization) and set the parent of that context to the ApplicationContext loaded by the TCF.

Stop invoking AnnotationConfigUtils.registerAnnotationConfigProcessors() in:

AbstractGenericContextLoader

AbstractGenericWebContextLoader

Ensure that child contexts are properly closed with regard to context caching in the TCF.

Original Issue Summary

Do not enable annotation-driven DI for the entire ApplicationContext in the TestContext framework

Original Proposal

It would be better if the TestContext framework (TCF) only enabled annotation-driven dependency injection for test instances and not for the entire ApplicationContext.

Affects: 2.5.2

Attachments:

AnnotationConfigProcessorTestExecutionListener.java (1.42 kB)

Issue Links:

#13149 Spring Test should not modify the application context under test ("is duplicated by")

#14496 Support WebApplicationContext hierarchies in the TestContext Framework

#10284 Provide support for context hierarchies in the TestContext Framework

#18295 Support 'throw-away' context for a test class

#10719 Provide mechanism for disabling automatic annotation-driven autowiring in tests

#14113 Test instances should not be proxied in the TestContext framework

6 votes, 6 watchers