Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-4632

Load dedicated child ApplicationContext for test instance in the TestContext framework

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 2.5.2
    • Fix Version/s: General Backlog
    • Component/s: Test
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      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 SPR-9478.


      Deliverables

      1. 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.
      2. Stop invoking AnnotationConfigUtils.registerAnnotationConfigProcessors() in:
        • AbstractGenericContextLoader
        • AbstractGenericWebContextLoader
      3. 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.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                sbrannen Sam Brannen
                Reporter:
                eberhardwolff Eberhard Wolff
                Last updater:
                Juergen Hoeller
              • Votes:
                5 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Days since last comment:
                  4 years, 11 weeks, 5 days ago