アノテーションインターフェース ContextHierarchy
@ContextHierarchy
は、テストクラスに適用して統合テスト用の ApplicationContexts
の階層を定義できるアノテーションです。サンプル
次の JUnit ベースの例は、コンテキスト階層の使用を必要とする統合テストの一般的な構成シナリオを示しています。
Single Test Class with Context Hierarchy
ControllerIntegrationTests
は、ルート WebApplicationContext
( TestAppConfig
を使用) 用とディスパッチャーサーブレット WebApplicationContext
( WebConfig
を使用) の 2 つのレベルで構成されるコンテキスト階層を宣言することにより、Spring MVC Web アプリケーションの典型的な統合テストシナリオを表します。テストインスタンスにオートワイヤーされる WebApplicationContext
は、子コンテキスト (つまり、階層の最下位コンテキスト) 用の WebApplicationContext
です。
@ExtendWith(SpringExtension.class) @WebAppConfiguration @ContextHierarchy({ @ContextConfiguration(classes = TestAppConfig.class), @ContextConfiguration(classes = WebConfig.class) }) class ControllerIntegrationTests { @Autowired WebApplicationContext wac; // ... }
Class Hierarchy with Implicit Parent Context
次のテストクラスは、テストクラス階層内のコンテキスト階層を定義します。AbstractWebTests
は、Spring を利用した Web アプリケーションでルート WebApplicationContext
の構成を宣言します。ただし、AbstractWebTests
は @ContextHierarchy
を宣言していないことに注意してください。AbstractWebTests
のサブクラスは、オプションでコンテキスト階層に参加するか、@ContextConfiguration
の標準セマンティクスに従うことができます。SoapWebServiceTests
と RestWebServiceTests
はどちらも AbstractWebTests
を継承し、@ContextHierarchy
を介してコンテキスト階層を定義します。その結果、3 つのアプリケーションコンテキストがロードされ(@ContextConfiguration
の宣言ごとに 1 つ)、AbstractWebTests
の構成に基づいてロードされたアプリケーションコンテキストが、具象サブクラスにロードされた各コンテキストの親コンテキストとして設定されます。
@ExtendWith(SpringExtension.class) @WebAppConfiguration @ContextConfiguration("file:src/main/webapp/WEB-INF/applicationContext.xml") public abstract class AbstractWebTests {} @ContextHierarchy(@ContextConfiguration("/spring/soap-ws-config.xml")) public class SoapWebServiceTests extends AbstractWebTests {} @ContextHierarchy(@ContextConfiguration("/spring/rest-ws-config.xml")) public class RestWebServiceTests extends AbstractWebTests {}
Class Hierarchy with Merged Context Hierarchy Configuration
次のクラスは、コンテキスト階層内の特定のレベルの構成をマージするための名前付き階層レベルの使用箇所を示しています。BaseTests
は、階層内に parent
と child
の 2 つのレベルを定義します。ExtendedTests
は BaseTests
を継承し、Spring TestContext フレームワークに、ContextConfiguration.name()
を介して宣言された名前が両方とも "child"
であることを確認するだけで、child
階層レベルのコンテキスト構成をマージするように指示します。その結果、3 つのアプリケーションコンテキストがロードされます。1 つは "/app-config.xml"
、1 つは "/user-config.xml"
、1 つは {"/user-config.xml", "/order-config.xml"}
です。前の例と同様に、"/app-config.xml"
からロードされたアプリケーションコンテキストは、"/user-config.xml"
および {"/user-config.xml", "/order-config.xml"}
からロードされたコンテキストの親コンテキストとして設定されます。
@ExtendWith(SpringExtension.class) @ContextHierarchy({ @ContextConfiguration(name = "parent", locations = "/app-config.xml"), @ContextConfiguration(name = "child", locations = "/user-config.xml") }) public class BaseTests {} @ContextHierarchy( @ContextConfiguration(name = "child", locations = "/order-config.xml") ) public class ExtendedTests extends BaseTests {}
Class Hierarchy with Overridden Context Hierarchy Configuration
前の例とは対照的に、この例は、ContextConfiguration.inheritLocations()
フラグを false
に設定することにより、コンテキスト階層内の特定の名前付きレベルの構成をオーバーライドする方法を示しています。その結果、ExtendedTests
のアプリケーションコンテキストは "/test-user-config.xml"
からのみ読み込まれ、その親は "/app-config.xml"
から読み込まれたコンテキストに設定されます。
@ExtendWith(SpringExtension.class) @ContextHierarchy({ @ContextConfiguration(name = "parent", locations = "/app-config.xml"), @ContextConfiguration(name = "child", locations = "/user-config.xml") }) public class BaseTests {} @ContextHierarchy( @ContextConfiguration(name = "child", locations = "/test-user-config.xml", inheritLocations = false) ) public class ExtendedTests extends BaseTests {}
このアノテーションは、カスタム構成アノテーションを作成するためのメタアノテーションとして使用することができます。
Spring Framework 5.3 以降、このアノテーションはデフォルトでそれを囲むテストクラスから継承されます。詳細については、@NestedTestConfiguration
を参照してください。
- 導入:
- 3.2.2
- 作成者:
- Sam Brannen
- 関連事項:
必須定数のサマリー
必須要素
要素の詳細
value
ContextConfiguration[] value@ContextConfiguration
インスタンスのリスト。各インスタンスは、コンテキスト階層のレベルを定義します。テストクラス階層内のコンテキスト階層の特定のレベルの構成をマージまたはオーバーライドする必要がある場合は、クラス階層の各レベルで
@ContextConfiguration
のname
属性に同じ値を指定して、そのレベルに明示的に名前を付ける必要があります。例については、クラスレベルの Javadoc を参照してください。