上下文配置继承
@ContextConfiguration
支持布尔值inheritLocations
和inheritInitializers
属性,表示是资源位置还是组件类和上下文应继承超类声明的初始值设定项。两个flags 的默认值为true
. 这意味着测试类继承资源位置或组件类以及任何超类声明的上下文初始值设定项。具体来说,测试类的资源位置或组件类被附加到超类声明的资源位置或带注释的类的列表中。同样,给定测试类的初始值设定项被添加到初始值设定项集由测试超类定义。因此,子类可以选择扩展资源位置、组件类或上下文初始值设定项。
如果inheritLocations
或inheritInitializers
属性@ContextConfiguration
设置为false
、资源位置或组件类和上下文初始值设定项,分别用于测试类 shadow,并有效地替换超类定义的配置。
测试配置也可以从封闭类继承。 看@Nested 测试类配置了解详情。 |
在下一个使用 XML 资源位置的示例中,ApplicationContext
为ExtendedTest
加载自base-config.xml
和extended-config.xml
,按此顺序。在extended-config.xml
因此,可以覆盖(即替换)那些定义在base-config.xml
. 以下示例显示了一个类如何扩展另一个类,并同时使用自己的配置文件和超类的配置文件:
-
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 | 子类中定义的配置文件。 |
同样,在下一个使用组件类的示例中,ApplicationContext
为ExtendedTest
从BaseConfig
和ExtendedConfig
类,在 次序。 在ExtendedConfig
因此,可以覆盖(即替换)中定义的BaseConfig
. 以下示例显示了一个类如何扩展另一个类,并同时使用自己的配置类和超类的配置类:
-
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 | 子类中定义的配置类。 |
在下一个使用上下文初始值设定项的示例中,ApplicationContext
为ExtendedTest
通过使用BaseInitializer
和ExtendedInitializer
. 注意 但是,调用初始值设定项的顺序取决于它们是否实现 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 | 子类中定义的初始值设定项。 |