|
对于最新稳定版本,请使用Spring Framework 7.0.1! |
上下文配置继承
@ContextConfiguration支持布尔值继承地点和inheritInitializers表示资源位置还是组件类和上下文的属性
由超类声明的初始化器应继承。两者的默认值
旗帜是true.这意味着测试类继承资源位置,或者
组件类以及由任何超类声明的上下文初始化器。
具体来说,测试类的资源位置或组件类会被附加
到资源位置列表或由超类声明的注释类。
同样,给定测试类的初始化器也会被添加到初始化器的集合中
由测试超类定义。因此,子类可以选择扩展资源
位置、组件类或上下文初始化器。
如果继承地点或inheritInitializers属性@ContextConfiguration设置为false,资源位置或组件类以及上下文
分别是测试类 Shadow 和 的初始化器,有效地替代了
配置由超类定义。
从 Spring Framework 5.3 起,测试配置也可以继承自随附的版本
类。看@Nested测试类配置细节。 |
在下一个例子中,使用 XML 资源位置,应用上下文为扩展测试是从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 | 子类中定义的配置文件。 |
同样,在下一个使用组件类的例子中,应用上下文为扩展测试是从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 | 配置类定义在子类中。 |
在下一个使用上下文初始化器的例子中,应用上下文为扩展测试初始化为:BaseInitializer和扩展初始化器.注意
然而,初始化器的调用顺序取决于它们是否
实现了Spring的命令界面或用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 | 初始化器定义在子类中。 |