アノテーションインターフェース 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 {}Context Hierarchies with Bean Overrides
@ContextHierarchy を Bean オーバーライド(@TestBean、@MockitoBean、@MockitoSpyBean など)と組み合わせて使用する場合、オーバーライドをコンテキスト階層内の単一レベルに適用することが望ましい、または必要な場合があります。そのためには、Bean オーバーライドで、ContextConfiguration.name() で設定された名前と一致するコンテキスト名を指定する必要があります。
以下のテストクラスは、2 番目の階層レベルの名前を "user-config" に設定し、同時に UserService を "user-config" というコンテキスト内の Mockito スパイにラップするよう指定しています。その結果、Spring は "user-config" コンテキスト内でのみスパイの作成を試み、親コンテキスト内ではスパイの作成を試みません。
@ExtendWith(SpringExtension.class)
@ContextHierarchy({
@ContextConfiguration(classes = AppConfig.class),
@ContextConfiguration(classes = UserConfig.class, name = "user-config")
})
class IntegrationTests {
@MockitoSpyBean(contextName = "user-config")
UserService userService;
// ...
} コンテキスト階層の異なるレベルで Bean オーバーライドを適用する場合、モックのスタブを設定するなど、Bean オーバーライドインスタンスをすべてテストクラスに挿入して操作する必要がある場合があります。ただし、@Autowired は常に、コンテキスト階層の最下位レベルにある一致する Bean を注入します。コンテキスト階層の特定のレベルから Bean オーバーライドインスタンスを挿入するには、適切な Bean オーバーライドアノテーションをフィールドに付与し、コンテキストレベルの名前を設定する必要があります。
以下のテストクラスでは、階層レベルの名前を "parent" と "child" に設定しています。また、2 つの PropertyService フィールドを宣言し、それぞれのコンテキストで PropertyService Bean を Mockito モック("parent" および "child")に作成または置換するように設定しています。その結果、"parent" コンテキストのモックは propertyServiceInParent フィールドに、"child" コンテキストのモックは propertyServiceInChild フィールドに注入されます。
@ExtendWith(SpringExtension.class)
@ContextHierarchy({
@ContextConfiguration(classes = ParentConfig.class, name = "parent"),
@ContextConfiguration(classes = ChildConfig.class, name = "child")
})
class IntegrationTests {
@MockitoBean(contextName = "parent")
PropertyService propertyServiceInParent;
@MockitoBean(contextName = "child")
PropertyService propertyServiceInChild;
// ...
}Miscellaneous
このアノテーションは、カスタム構成アノテーションを作成するためのメタアノテーションとして使用することができます。
このアノテーションは、デフォルトで、囲んでいるテストクラスから継承されます。詳細については、@NestedTestConfiguration を参照してください。
- 導入:
- 3.2.2
- 作成者:
- Sam Brannen
- 関連事項:
必須定数のサマリー
必須要素
要素の詳細
value
ContextConfiguration[] value@ContextConfigurationインスタンスのリスト。各インスタンスは、コンテキスト階層のレベルを定義します。テストクラス階層内のコンテキスト階層の特定のレベルの構成をマージまたはオーバーライドする必要がある場合は、クラス階層の各レベルで
@ContextConfigurationのname属性に同じ値を指定して、そのレベルに明示的に名前を付ける必要があります。例については、クラスレベルの Javadoc を参照してください。