Skip to content

Provide support for ApplicationContextInitializers in the TestContext framework [SPR-9011] #13650

Closed
@spring-projects-issues

Description

@spring-projects-issues

Rossen Stoyanchev opened SPR-9011 and commented

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

[x] Introduce a new initializers attribute in @ContextConfiguration.


     ```

Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[] initializers() default {};

1. [x] Introduce a new `inheritInitializers` attribute in `@ContextConfiguration`.
   - ```
boolean inheritInitializers() default true;
  1. Allow a context to be loaded solely via a custom ApplicationContextInitializer (i.e., without locations or classes)

  2. Initializers must be included in MergedContextConfiguration for determining the context cache key.

  3. Invoke initializers within existing SmartContextLoader implementations.

    • for example, in AbstractGenericContextLoader.loadContext(...) methods

    [x] 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());

1. [x] Document in Javadoc
1. [x] Document in the reference manual

----
#### Pseudocode Examples

```java 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
    locations = "/app-config.xml",
    initializers = CustomInitializer.class)
public class ApplicationContextInitializerTests {}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
    locations = "/app-config.xml",
    initializers = {PropertySourceInitializer.class, ProfileInitializer.class})
public class ApplicationContextInitializerTests {}
@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 {}
@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 {}
// 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  {}

Affects: 3.1 GA

Referenced from: commits 1f93777, 58daeea

11 votes, 15 watchers

Metadata

Metadata

Assignees

Labels

has: votes-jiraIssues migrated from JIRA with more than 10 votes at the time of importin: testIssues in the test moduletype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions