上下文配置继承

@ContextConfiguration 支持布尔类型的 inheritLocationsinheritInitializers 属性,用于指示是否应继承超类中声明的资源位置或组件类和上下文初始化器。这两个标志的默认值都是 true。这意味着测试类会继承任何超类中声明的资源位置或组件类以及上下文初始化器。具体来说,测试类的资源位置或组件类会附加到超类中声明的资源位置或注解类列表之后。类似地,给定测试类的初始化器会被添加到测试超类定义的初始化器集合中。因此,子类可以选择扩展资源位置、组件类或上下文初始化器。

如果 @ContextConfiguration 中的 inheritLocationsinheritInitializers 属性设置为 false,则测试类的资源位置或组件类以及上下文初始化器将分别遮盖并有效地替换超类中定义的配置。

测试配置也可以从包含类继承。详情请参阅 @Nested 测试类配置

在下面的示例中,使用了 XML 资源位置,ExtendedTestApplicationContext 将按顺序从 base-config.xmlextended-config.xml 加载。因此,在 extended-config.xml 中定义的 Bean 可以覆盖(即替换)在 base-config.xml 中定义的 Bean。以下示例展示了一个类如何扩展另一个类并同时使用自己的配置文件和超类的配置文件

  • Java

  • Kotlin

@ExtendWith(SpringExtension.class)
// ApplicationContext will be loaded from "/base-config.xml"
// in the root of the classpath
@ContextConfiguration("/base-config.xml") (1)
class BaseTest {
	// class body...
}

// ApplicationContext will be loaded from "/base-config.xml" and
// "/extended-config.xml" in the root of the classpath
@ContextConfiguration("/extended-config.xml") (2)
class ExtendedTest extends BaseTest {
	// class body...
}
1 在超类中定义的配置文件。
2 在子类中定义的配置文件。
@ExtendWith(SpringExtension::class)
// ApplicationContext will be loaded from "/base-config.xml"
// in the root of the classpath
@ContextConfiguration("/base-config.xml") (1)
open class BaseTest {
	// class body...
}

// ApplicationContext will be loaded from "/base-config.xml" and
// "/extended-config.xml" in the root of the classpath
@ContextConfiguration("/extended-config.xml") (2)
class ExtendedTest : BaseTest() {
	// class body...
}
1 在超类中定义的配置文件。
2 在子类中定义的配置文件。

类似地,在下面的示例中,使用了组件类,ExtendedTestApplicationContext 将按顺序从 BaseConfigExtendedConfig 类加载。因此,在 ExtendedConfig 中定义的 Bean 可以覆盖(即替换)在 BaseConfig 中定义的 Bean。以下示例展示了一个类如何扩展另一个类并同时使用自己的配置类和超类的配置类

  • Java

  • Kotlin

// ApplicationContext will be loaded from BaseConfig
@SpringJUnitConfig(BaseConfig.class) (1)
class BaseTest {
	// class body...
}

// ApplicationContext will be loaded from BaseConfig and ExtendedConfig
@SpringJUnitConfig(ExtendedConfig.class) (2)
class ExtendedTest extends BaseTest {
	// class body...
}
1 在超类中定义的配置类。
2 在子类中定义的配置类。
// ApplicationContext will be loaded from BaseConfig
@SpringJUnitConfig(BaseConfig::class) (1)
open class BaseTest {
	// class body...
}

// ApplicationContext will be loaded from BaseConfig and ExtendedConfig
@SpringJUnitConfig(ExtendedConfig::class) (2)
class ExtendedTest : BaseTest() {
	// class body...
}
1 在超类中定义的配置类。
2 在子类中定义的配置类。

在下面的示例中,使用了上下文初始化器,ExtendedTestApplicationContext 将通过使用 BaseInitializerExtendedInitializer 进行初始化。然而请注意,初始化器的调用顺序取决于它们是否实现了 Spring 的 Ordered 接口,或者是否使用 Spring 的 @Order 注解或标准 @Priority 注解进行了标注。以下示例展示了一个类如何扩展另一个类并同时使用自己的初始化器和超类的初始化器

  • Java

  • Kotlin

// ApplicationContext will be initialized by BaseInitializer
@SpringJUnitConfig(initializers = BaseInitializer.class) (1)
class BaseTest {
	// class body...
}

// ApplicationContext will be initialized by BaseInitializer
// and ExtendedInitializer
@SpringJUnitConfig(initializers = ExtendedInitializer.class) (2)
class ExtendedTest extends BaseTest {
	// class body...
}
1 在超类中定义的初始化器。
2 在子类中定义的初始化器。
// ApplicationContext will be initialized by BaseInitializer
@SpringJUnitConfig(initializers = [BaseInitializer::class]) (1)
open class BaseTest {
	// class body...
}

// ApplicationContext will be initialized by BaseInitializer
// and ExtendedInitializer
@SpringJUnitConfig(initializers = [ExtendedInitializer::class]) (2)
class ExtendedTest : BaseTest() {
	// class body...
}
1 在超类中定义的初始化器。
2 在子类中定义的初始化器。