上下文配置繼承

@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 子類中定義的初始化器。
© . This site is unofficial and not affiliated with VMware.