Skip to content

Cannot override initializer of @ContextConfiguration when using @ContextHierarchy [SPR-10997] #15625

@spring-projects-issues

Description

@spring-projects-issues

Eric E opened SPR-10997 and commented

Overview

In the @ContextHierarchy uniqueness check, it should take into account the inherited parts; however, it currently ignores the inherited "locations" (i.e., the merged configuration). So the configuration declared in a subclass (see code examples below) appears to be non-unique.

Note that when I forgo inheritance of @ContextHierarchy entirely (comment it out of AbstractWebTests and put the complete annotation including initializers into the concrete class), then all of the tests pass.


Steps to Reproduce

I have these two classes, declared like so:

@ActiveProfiles(profiles={"testing"})
@ContextHierarchy({
  @ContextConfiguration(name="rootContext", locations={"classpath:app-config.xml", "classpath:database-config.xml", "classpath:security-config.xml"}),
  @ContextConfiguration(name="webContext", locations={"classpath:mvc-config.xml"})
})
@WebAppConfiguration
public abstract class AbstractWebTests extends
AbstractTransactionalJUnit4SpringContextTests {
}
public class PropertyMockingApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        // details omitted
    }

    // details omitted
}
@ContextHierarchy({
  @ContextConfiguration(name="rootContext", inheritInitializers=false, initializers=PropertyMockingApplicationContextInitializer.class),
  @ContextConfiguration(name="webContext", inheritInitializers=false, initializers=PropertyMockingApplicationContextInitializer.class)
})
public class UserManagementIntegrationTest extends AbstractWebTests {
    // details omitted
}

What I am trying to do here is have some generic configuration in my AbstractWebTests class, and then provide some specific initialization in the concrete test classes. I thought that I would be able to just override the initializer part of the @ContextHierarchy, but have no luck so far.

Unfortunately when I do the above, I get this:

2013-10-16 12:26:20,046 ERROR [org.springframework.test.context.ContextLoaderUtils] - The @ContextConfiguration elements configured via @ContextHierarchy in test class [ca.gc.health.istop.um.UserManagementIntegrationTest] must define unique contexts to load.

Affects: 3.2.4

Reference URL: http://forum.spring.io/forum/spring-projects/container/724288-trouble-overriding-initializer-in-contexthierarchy-in-subclass

Sub-tasks:

Referenced from: commits 7658d85

Metadata

Metadata

Assignees

Labels

in: testIssues in the test moduletype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions