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

Provide support for ApplicationContextInitializers in the TestContext framework

    Details

    • 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:
      None
    • Last commented by a User:
      false

      Description

      Status Quo

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


      Goals

      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.


      Deliverables

      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
      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(
          locations = "/app-config.xml",
          initializers = CustomInitializer.class)
      public class ApplicationContextInitializerTests {}
      
      Multiple Initializers
      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(
          locations = "/app-config.xml",
          initializers = {PropertySourceInitializer.class, ProfileInitializer.class})
      public class ApplicationContextInitializerTests {}
      
      Merged Initializers
      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(
          classes = BaseConfig.class,
          initializers = BaseInitializer.class)
      public class BaseTest {}
      
      @ContextConfiguration(
          classes = ExtendedConfig.class,
          initializers = ExtendedInitializer.class)
      public class ExtendedTest extends BaseTest {}
      
      Overridden Initializers
      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(
          classes = BaseConfig.class,
          initializers = BaseInitializer.class)
      public class BaseTest {}
      
      @ContextConfiguration(
          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  {}
      

        Attachments

          Activity

            People

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

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                5 years, 41 weeks ago

                Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0d
                0d
                Logged:
                Time Spent - 4.5d
                4.5d