クラス SpringExtension
- 実装されているすべてのインターフェース:
AfterAllCallback, AfterEachCallback, AfterTestExecutionCallback, BeforeAllCallback, BeforeEachCallback, BeforeTestExecutionCallback, Extension, ParameterResolver, TestInstancePostProcessor, TestInstantiationAwareExtension
SpringExtension は、Spring、TestContext、フレームワークを JUnit Jupiter テストフレームワークに統合します。 この拡張機能を使用するには、JUnit Jupiter ベースのテストクラスに @ExtendWith(SpringExtension.class)、@SpringJUnitConfig、@SpringJUnitWebConfig をアノテーションするか、@ExtendWith(SpringExtension.class) (@SpringBootTest など)でメタアノテーションされたその他のアノテーションをアノテーションします。
Spring Framework 7.0 以降、SpringExtension はテストメソッドスコープの ExtensionContext を使用するように構成されています。これにより、@Nested テストクラス階層内の現在のテストメソッドに対して、ApplicationContext のフィールドとコンストラクターへの一貫した依存性注入が可能になります。ただし、サードパーティ製の TestExecutionListener がテストメソッドスコープの拡張コンテキストに関連付けられたセマンティクスと互換性がない場合、または開発者がテストクラススコープのセマンティクスに切り替えたい場合は、最上位テストクラスに @SpringExtensionConfig(useTestClassScopedExtensionContext = true) アノテーションを付与することで、SpringExtension をテストクラススコープの拡張コンテキストを使用するように構成できます。
- 導入:
- 5.0
- 作成者:
- Sam Brannen, Simon Basl é
- 関連事項:
ネストされたクラスの要約
インターフェース TestInstantiationAwareExtension から継承されたネストクラス / インターフェース
TestInstantiationAwareExtension.ExtensionContextScopeコンストラクターの概要
コンストラクターメソッドのサマリー
修飾子と型メソッド説明voidafterAll(ExtensionContext context) TestContextManager.afterTestClass()へのデリゲート。voidafterEach(ExtensionContext context) voidafterTestExecution(ExtensionContext context) voidbeforeAll(ExtensionContext context) TestContextManager.beforeTestClass()へのデリゲート。voidbeforeEach(ExtensionContext context) voidbeforeTestExecution(ExtensionContext context) static ApplicationContextgetApplicationContext(ExtensionContext context) 指定されたExtensionContextに関連付けられたApplicationContextを取得します。voidpostProcessTestInstance(ObjectSE testInstance, ExtensionContext context) resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) booleansupportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
コンストラクターの詳細
SpringExtension
public SpringExtension()
メソッドの詳細
getTestInstantiationExtensionContextScope
public TestInstantiationAwareExtension.ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) ExtensionContextScope.TEST_METHODを返します。This can be effectively overridden by annotating a test class with
@SpringExtensionConfig(useTestClassScopedExtensionContext = true). See the class-level Javadoc for further details.- 次で指定:
- インターフェース
TestInstantiationAwareExtensionのgetTestInstantiationExtensionContextScope - 導入:
- 7.0
- 関連事項:
beforeAll
TestContextManager.beforeTestClass()へのデリゲート。- 次で指定:
- インターフェース
BeforeAllCallbackのbeforeAll - 例外:
ExceptionSE
afterAll
TestContextManager.afterTestClass()へのデリゲート。- 次で指定:
- インターフェース
AfterAllCallbackのafterAll - 例外:
ExceptionSE
postProcessTestInstance
public void postProcessTestInstance(ObjectSE testInstance, ExtensionContext context) throws ExceptionSE TestContextManager.prepareTestInstance(Object)へのデリゲート。このメソッドは、テストメソッドとテストライフサイクルメソッドに
@Autowiredアノテーションが付けられていないことも検証します。- 次で指定:
- インターフェース
TestInstancePostProcessorのpostProcessTestInstance - 例外:
ExceptionSE
beforeEach
- 次で指定:
- インターフェース
BeforeEachCallbackのbeforeEach - 例外:
ExceptionSE
beforeTestExecution
- 次で指定:
- インターフェース
BeforeTestExecutionCallbackのbeforeTestExecution - 例外:
ExceptionSE
afterTestExecution
- 次で指定:
- インターフェース
AfterTestExecutionCallbackのafterTestExecution - 例外:
ExceptionSE
afterEach
- 次で指定:
- インターフェース
AfterEachCallbackのafterEach - 例外:
ExceptionSE
supportsParameter
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) 提供されたParameterContextのParameterSE の値をテストのApplicationContextからオートワイヤーする必要があるかどうかを決定します。次のいずれかの条件が
trueの場合、パラメーターは自動書き込み可能と見なされます。- 宣言している実行可能ファイルは
ConstructorSE であり、TestConstructorUtils.isAutowirableConstructor(Executable, PropertyProvider)はtrueを返します。isAutowirableConstructor()は、ルックアップをExtensionContext.getConfigurationParameter(String)に委譲するフォールバックPropertyProviderで呼び出されることに注意してください。 - パラメーターは型
ApplicationContextまたはそのサブ型です。 - パラメーターは型
ApplicationEventsまたはそのサブ型です。 ParameterResolutionDelegate.isAutowirable(Parameter, int)はtrueを返します。
This method does not load the
ApplicationContextor verify that the application context actually contains a matching candidate bean, since doing so would potentially load an application context too early or unnecessarily.警告 : テストクラス
Constructorに@Autowiredのアノテーションが付けられているか、自動的に自動書き込み可能(@TestConstructorを参照)の場合、Spring がコンストラクター内のすべてのパラメーターを解決する責任を負います。その結果、他の登録済みParameterResolverはパラメーターを解決できなくなります。- 次で指定:
- インターフェース
ParameterResolverのsupportsParameter - 関連事項:
- 宣言している実行可能ファイルは
resolveParameter
public @Nullable ObjectSE resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) テストのApplicationContextから対応する依存関係を取得することにより、提供されたParameterContextのParameterSE の値を解決します。ParameterResolutionDelegate.resolveDependency(Parameter, int, Class, AutowireCapableBeanFactory)へのデリゲート。- 次で指定:
- インターフェース
ParameterResolverのresolveParameter - 関連事項:
getApplicationContext
指定されたExtensionContextに関連付けられたApplicationContextを取得します。NOTE: As of Spring Framework 7.0, the supplied
ExtensionContextmay not be properly scoped. See the class-level Javadoc for further details.WARNING: Invoking this method ensures that the corresponding
ApplicationContextis loaded. Consequently, this method should not be used if eager loading of the application context is undesired. For example,supportsParameter(ParameterContext, ExtensionContext)intentionally does not invoke this method, since doing so would potentially load an application context too early or unnecessarily.- パラメーター:
context- 現在のExtensionContext(非null)- 戻り値:
- アプリケーションコンテキスト
- 例外:
IllegalStateExceptionSE- アプリケーションコンテキストの取得中にエラーが発生した場合- 関連事項: