上下文配置繼承
@ContextConfiguration
支援布林型別的 inheritLocations
和 inheritInitializers
屬性,用於指示是否應繼承超類中宣告的資源位置或元件類和上下文初始化器。這兩個標誌的預設值都是 true
。這意味著測試類會繼承任何超類中宣告的資源位置或元件類以及上下文初始化器。具體來說,測試類的資源位置或元件類會附加到超類中宣告的資源位置或註解類列表之後。類似地,給定測試類的初始化器會被新增到測試超類定義的初始化器集合中。因此,子類可以選擇擴充套件資源位置、元件類或上下文初始化器。
如果 @ContextConfiguration
中的 inheritLocations
或 inheritInitializers
屬性設定為 false
,則測試類的資源位置或元件類以及上下文初始化器將分別遮蓋並有效地替換超類中定義的配置。
測試配置也可以從包含類繼承。詳情請參閱 @Nested 測試類配置。 |
在下面的示例中,使用了 XML 資源位置,ExtendedTest
的 ApplicationContext
將按順序從 base-config.xml
和 extended-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 | 在子類中定義的配置檔案。 |
類似地,在下面的示例中,使用了元件類,ExtendedTest
的 ApplicationContext
將按順序從 BaseConfig
和 ExtendedConfig
類載入。因此,在 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 | 在子類中定義的配置類。 |
在下面的示例中,使用了上下文初始化器,ExtendedTest
的 ApplicationContext
將透過使用 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 | 在子類中定義的初始化器。 |