此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10! |
上下文配置继承
@ContextConfiguration
支持布尔值inheritLocations
和inheritInitializers
表示资源位置还是组件类和上下文的属性
应继承超类声明的初始值设定项。两者的默认值
flags 是true
.这意味着测试类继承资源位置或
组件类以及任何超类声明的上下文初始值设定项。
具体来说,将附加测试类的资源位置或组件类
到超类声明的资源位置或带注释的类的列表。
同样,给定测试类的初始值设定项将添加到初始值设定项集
由测试超类定义。因此,子类可以选择扩展资源
位置、组件类或上下文初始值设定项。
如果inheritLocations
或inheritInitializers
属性@ContextConfiguration
设置为false
、资源位置或组件类以及上下文
初始值设定项,并有效地替换
由超类定义的配置。
测试配置也可以从封闭类继承。看@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 | 子类中定义的初始值设定项。 |