引導 TestContext 框架
對於所有常見用例,Spring TestContext 框架內部結構的預設配置已經足夠。但是,有時開發團隊或第三方框架可能希望更改預設的 ContextLoader
、實現自定義的 TestContext
或 ContextCache
、增強預設的 ContextCustomizerFactory
和 TestExecutionListener
實現集合等。為了對 TestContext 框架的操作方式進行這種低階控制,Spring 提供了引導策略。
TestContextBootstrapper
定義了引導 TestContext 框架的 SPI。TestContextManager
使用 TestContextBootstrapper
來載入當前測試的 TestExecutionListener
實現,並構建它管理的 TestContext
。您可以透過直接或作為元註解使用 @BootstrapWith
為測試類(或測試類層次結構)配置自定義引導策略。如果未使用 @BootstrapWith
明確配置載入程式,則將使用 DefaultTestContextBootstrapper
或 WebTestContextBootstrapper
,具體取決於是否存在 @WebAppConfiguration
。
由於 TestContextBootstrapper
SPI 未來可能會更改(以適應新的需求),我們強烈建議實現者不要直接實現此介面,而是擴充套件 AbstractTestContextBootstrapper
或其具體子類之一。