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

Provide support for ApplicationContextInitializers in the TestContext framework


    • Type: New Feature
    • Status: Resolved
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 3.1 GA
    • Fix Version/s: 3.2 M2
    • Component/s: Test
    • Labels:
    • Last commented by a User:


      Status Quo

      Starting with Spring 3.1 applications can specify contextInitializerClasses via context-param and init-param in web.xml.


      For comprehensive testing it should be possible to re-use ApplicationContextInitializer instances in tests as well.

      This could be done at the @ContextConfiguration level by allowing an array of ACI types to be specified, and the TCF would allow each to visit the ApplicationContext at the right time.


      1. Introduce a new initializers attribute in @ContextConfiguration.
        • Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[] initializers() default {};
      2. Introduce a new inheritInitializers attribute in @ContextConfiguration.
        • boolean inheritInitializers() default true;
      3. Allow a context to be loaded solely via a custom ApplicationContextInitializer (i.e., without locations or classes)
      4. Initializers must be included in MergedContextConfiguration for determining the context cache key.
      5. Invoke initializers within existing SmartContextLoader implementations.
        • for example, in AbstractGenericContextLoader.loadContext(...) methods
        • per the contract defined in the Javadoc for ApplicationContextInitializer: ApplicationContextInitializer processors are encouraged to detect whether Spring's Ordered interface has been implemented or if the @Order annotation is present and to sort instances accordingly if so prior to invocation.
          • Collections.sort(initializerInstances, new AnnotationAwareOrderComparator());
      6. Document in Javadoc
      7. Document in the reference manual

      Pseudocode Examples

      Single Initializer
          locations = "/app-config.xml",
          initializers = CustomInitializer.class)
      public class ApplicationContextInitializerTests {}
      Multiple Initializers
          locations = "/app-config.xml",
          initializers = {PropertySourceInitializer.class, ProfileInitializer.class})
      public class ApplicationContextInitializerTests {}
      Merged Initializers
          classes = BaseConfig.class,
          initializers = BaseInitializer.class)
      public class BaseTest {}
          classes = ExtendedConfig.class,
          initializers = ExtendedInitializer.class)
      public class ExtendedTest extends BaseTest {}
      Overridden Initializers
          classes = BaseConfig.class,
          initializers = BaseInitializer.class)
      public class BaseTest {}
          classes = ExtendedConfig.class,
          initializers = ExtendedInitializer.class,
          inheritInitializers = false)
      public class ExtendedTest extends BaseTest {}
      Initializer without Locations or Classes
      // In the following example, an exception would not be thrown even
      // if no default XML file or @Configuration class is detected.
      // In other words, the initializer would be responsible for 
      // providing XML configuration files or annotated configuration
      // classes to the provided context.
      @ContextConfiguration(initializers = EntireAppInitializer.class)
      public class InitializerWithoutConfigFilesOrClassesTest extends BaseTest  {}




            • Assignee:
              sbrannen Sam Brannen
              rstoya05-aop Rossen Stoyanchev
              Last updater:
              Sam Brannen
            • Votes:
              11 Vote for this issue
              15 Start watching this issue


              • Created:
                Days since last comment:
                5 years, 36 weeks, 2 days ago

                Time Tracking

                Original Estimate - Not Specified
                Not Specified
                Remaining Estimate - 0d
                Time Spent - 4.5d