@TargetSE(valueSE=TYPESE) @RetentionSE(valueSE=RUNTIMESE) @DocumentedSE @InheritedSE public @interface ContextHierarchy
@ContextHierarchy は、統合テスト用の ApplicationContexts の階層を定義するために使用されるクラスレベルのアノテーションです。次の JUnit ベースの例は、コンテキスト階層の使用を必要とする統合テストの一般的な構成シナリオを示しています。
ControllerIntegrationTests は、ルート WebApplicationContext ( TestAppConfig を使用) 用とディスパッチャーサーブレット WebApplicationContext ( WebConfig を使用) の 2 つのレベルで構成されるコンテキスト階層を宣言することにより、Spring MVC Web アプリケーションの典型的な統合テストシナリオを表します。テストインスタンスにオートワイヤーされる WebApplicationContext は、子コンテキスト (つまり、階層の最下位コンテキスト) 用の WebApplicationContext です。
@RunWith(SpringRunner.class)
@WebAppConfiguration
@ContextHierarchy({
@ContextConfiguration(classes = TestAppConfig.class),
@ContextConfiguration(classes = WebConfig.class)
})
public class ControllerIntegrationTests {
@Autowired
private WebApplicationContext wac;
// ...
} 次のテストクラスは、テストクラス階層内のコンテキスト階層を定義します。AbstractWebTests は、Spring 対応の Web アプリケーションでルート WebApplicationContext の構成を宣言します。ただし、AbstractWebTests は @ContextHierarchy を宣言しないことに注意してください。その結果、AbstractWebTests のサブクラスは、オプションでコンテキスト階層に参加したり、@ContextConfiguration の標準のセマンティクスに従うことができます。SoapWebServiceTests と RestWebServiceTests はどちらも AbstractWebTests を継承し、@ContextHierarchy を介してコンテキスト階層を定義します。その結果、3 つのアプリケーションコンテキスト(@ContextConfiguration の宣言ごとに 1 つ)が読み込まれ、AbstractWebTests の構成に基づいて読み込まれたアプリケーションコンテキストが、具象サブクラスに読み込まれた各コンテキストの親コンテキストとして設定されます。
@RunWith(SpringRunner.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 {} 次のクラスは、コンテキスト階層内の特定のレベルの構成をマージするための名前付き階層レベルの使用箇所を示しています。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"} からロードされたコンテキストの親コンテキストとして設定されます。
@RunWith(SpringRunner.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 {} 前の例とは対照的に、この例は、ContextConfiguration.inheritLocations() フラグを false に設定することにより、コンテキスト階層内の特定の名前付きレベルの構成をオーバーライドする方法を示しています。その結果、ExtendedTests のアプリケーションコンテキストは "/test-user-config.xml" からのみ読み込まれ、その親は "/app-config.xml" から読み込まれたコンテキストに設定されます。
@RunWith(SpringRunner.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 4.0 以降、このアノテーションをメタアノテーションとして使用して、カスタム合成アノテーションを作成できます。
ContextConfiguration, ApplicationContext| 修飾子と型 | 必須要素と説明 |
|---|---|
ContextConfiguration[] | value@ContextConfiguration インスタンスのリスト。各インスタンスは、コンテキスト階層のレベルを定義します。 |
public abstract ContextConfiguration[] value
@ContextConfiguration インスタンスのリスト。各インスタンスは、コンテキスト階層のレベルを定義します。 テストクラス階層内のコンテキスト階層の特定のレベルの構成をマージまたはオーバーライドする必要がある場合は、クラス階層の各レベルで @ContextConfiguration の name 属性に同じ値を指定して、そのレベルに明示的に名前を付ける必要があります。例については、クラスレベルの Javadoc を参照してください。