SELF - このランナーの「自己」型 C - コンテキスト型 A - アプリケーションコンテキストアサーションプロバイダー public abstract class AbstractApplicationContextRunner<SELF extends AbstractApplicationContextRunner<SELF,C,A>,C extends org.springframework.context.ConfigurableApplicationContext,A extends ApplicationContextAssertProvider<C>> extends ObjectSE
ApplicationContext を実行し、AssertJ スタイルのアサーションを提供するユーティリティ設計。テストは、テストに必要な共有構成を説明するテストクラスのフィールドとして最適に使用されます。
public class MyContextTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues("spring.foo=bar")
.withUserConfiguration(MyConfiguration.class);
} 上記の初期化により、すべてのテストで MyConfiguration を登録し、特に指定のない限り、spring.foo プロパティを bar に設定します。
上記の設定に基づいて、特定のテストは、おそらくオーバーライドされたプロパティ値を使用して、コンテキストの実行時に何が起こるかをシミュレートできます。
@Test
public someTest() {
this.contextRunner.withPropertyValues("spring.foo=biz").run((context) -> {
assertThat(context).containsSingleBean(MyBean.class);
// other assertions
});
} 上記のテストは、spring.foo プロパティを biz に変更し、コンテキストに単一の MyBean Bean が含まれることをアサートしています。run メソッドは、アサーションをコンテキストに適用できる ContextConsumer を取ります。完了すると、コンテキストは自動的に閉じられます。
アプリケーションコンテキストの起動に失敗すると、「失敗した」アプリケーションコンテキストで #run(ContextConsumer) メソッドが呼び出されます。コンテキストの呼び出しは IllegalStateExceptionSE をスローし、実行中のコンテキストを予期するアサーションは失敗します。失敗の原因でさらにチェックが必要な場合は、getFailure() アサーションを使用できます。
@Test
public someTest() {
this.context.withPropertyValues("spring.foo=fails").run((loaded) -> {
assertThat(loaded).getFailure().hasCauseInstanceOf(BadPropertyException.class);
// other assertions
});
}ApplicationContextRunner, WebApplicationContextRunner, ReactiveWebApplicationContextRunner, ApplicationContextAssert| 修飾子 | コンストラクターと説明 |
|---|---|
protected | AbstractApplicationContextRunner(SupplierSE<C> contextFactory) 新しい AbstractApplicationContextRunner インスタンスを作成します。 |
protected | AbstractApplicationContextRunner(SupplierSE<C> contextFactory, ListSE<org.springframework.context.ApplicationContextInitializer<? super C>> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoaderSE classLoader, org.springframework.context.ApplicationContext parent, ListSE<Configurations> configurations) 新しい AbstractApplicationContextRunner インスタンスを作成します。 |
| 修飾子と型 | メソッドと説明 |
|---|---|
protected abstract SELF | newInstance(SupplierSE<C> contextFactory, ListSE<org.springframework.context.ApplicationContextInitializer<? super C>> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoaderSE classLoader, org.springframework.context.ApplicationContext parent, ListSE<Configurations> configurations) |
SELF | run(ContextConsumer<? super A> consumer) このローダーの現在の状態に基づいて、新しい ApplicationContext を作成してリフレッシュします。 |
SELF | with(FunctionSE<SELF, SELF> customizer) このランナーにカスタマイズを適用します。 |
SELF | withClassLoader(ClassLoaderSE classLoader)ApplicationContext がリソースのロードと Bean クラスのロードに使用する ClassLoaderSE をカスタマイズします。 |
SELF | withConfiguration(Configurations configurations) 指定された構成クラスを ApplicationContext に登録します。 |
SELF | withInitializer(org.springframework.context.ApplicationContextInitializer<? super org.springframework.context.ConfigurableApplicationContext> initializer) コンテキストの作成時に呼び出される ApplicationContextInitializer を追加します。 |
SELF | withParent(org.springframework.context.ApplicationContext parent)ApplicationContext の parent を構成します。 |
SELF | withPropertyValues(StringSE... pairs) 指定された Environment プロパティペアを追加します。 |
SELF | withSystemProperties(StringSE... pairs) 指定された SystemSE プロパティペアを追加します。 |
SELF | withUserConfiguration(ClassSE<?>... configurationClasses) 指定されたユーザー構成クラスを ApplicationContext に登録します。 |
cloneSE, equalsSE, finalizeSE, getClassSE, hashCodeSE, notifySE, notifyAllSE, toStringSE, waitSE, waitSE, waitSEprotected AbstractApplicationContextRunner(SupplierSE<C> contextFactory)
AbstractApplicationContextRunner インスタンスを作成します。contextFactory - 実際のコンテキストの作成に使用されるファクトリ protected AbstractApplicationContextRunner(SupplierSE<C> contextFactory, ListSE<org.springframework.context.ApplicationContextInitializer<? super C>> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoaderSE classLoader, org.springframework.context.ApplicationContext parent, ListSE<Configurations> configurations)
AbstractApplicationContextRunner インスタンスを作成します。contextFactory - 実際のコンテキストの作成に使用されるファクトリ initializers - イニシャライザー environmentProperties - 環境プロパティ systemProperties - システムのプロパティ classLoader - クラスローダー parent - 親 configurations - 構成 public SELF withInitializer(org.springframework.context.ApplicationContextInitializer<? super org.springframework.context.ConfigurableApplicationContext> initializer)
ApplicationContextInitializer を追加します。initializer - 追加する初期化子 public SELF withPropertyValues(StringSE... pairs)
Environment プロパティペアを追加します。キーと値のペアは、コロン(":")または等号("=")の区切り文字で指定できます。以前に指定された可能性のある一致するキーをオーバーライドします。pairs - 環境に追加する必要があるプロパティのキーと値のペア TestPropertyValues, withSystemProperties(String...)public SELF withSystemProperties(StringSE... pairs)
SystemSE プロパティペアを追加します。キーと値のペアは、コロン(":")または等号("=")区切り文字で指定できます。コンテキストが run になる前にシステムプロパティが追加され、コンテキストが閉じられると復元されます。pairs - システムに追加する必要があるプロパティのキーと値のペア TestPropertyValues, withSystemProperties(String...)public SELF withClassLoader(ClassLoaderSE classLoader)
ApplicationContext がリソースのロードと Bean クラスのロードに使用する ClassLoaderSE をカスタマイズします。classLoader - 使用するクラスローダー (デフォルトを使用するために null にすることができます)FilteredClassLoaderpublic SELF withParent(org.springframework.context.ApplicationContext parent)
ApplicationContext の parent を構成します。parent - 親 public SELF withUserConfiguration(ClassSE<?>... configurationClasses)
ApplicationContext に登録します。configurationClasses - 追加するユーザー構成クラス public SELF withConfiguration(Configurations configurations)
ApplicationContext に登録します。configurations - 追加する構成 public SELF with(FunctionSE<SELF,SELF> customizer)
customizer - 呼び出すカスタマイザ protected abstract SELF newInstance(SupplierSE<C> contextFactory, ListSE<org.springframework.context.ApplicationContextInitializer<? super C>> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoaderSE classLoader, org.springframework.context.ApplicationContext parent, ListSE<Configurations> configurations)
public SELF run(ContextConsumer<? super A> consumer)
ApplicationContext を作成してリフレッシュします。コンテキストは指定された consumer によって消費され、完了時に閉じられます。consumer - 作成された ApplicationContext のコンシューマー Copyright © 2019 Pivotal Software, Inc.. All rights reserved.