spring-tests-override-properties
Springのテストでプロパティをオーバーライドする
-
link:/category/testing/ [テスト]
1. 概要
このチュートリアルでは、Springのテストでプロパティをオーバーライドするさまざまな方法を見ていきます。
実際、Springはこれに対して多くのソリューションを提供しているため、ここで詳しく検討する必要があります。
2. 依存関係
もちろん、Springテストを使用するには、テストの依存関係を追加する必要があります。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.6.RELEASE</version>
<scope>test</scope>
</dependency>
_https://search.maven.org/search?q = a:spring-boot-starter-test%20AND%20g:org.springframework.boot [spring-boot-starter-test] _ dependencyには必要なすべてが含まれていますテストでプロパティ値をオーバーライドします。
3. セットアップ
まず、アプリケーションでプロパティを使用するクラスを作成する必要があります。
@Component
public class PropertySourceResolver {
@Value("${example.firstProperty}") private String firstProperty;
@Value("${example.secondProperty}") private String secondProperty;
public String getFirstProperty() {
return firstProperty;
}
public String getSecondProperty() {
return secondProperty;
}
}
次に、それらに値を割り当てましょう。 それを行うには、main_src / main / resources:_に_application.properties_を作成します。
example.firstProperty=defaultFirst
example.secondProperty=defaultSecond
4. プロパティファイルのオーバーライド
次に、プロパティファイルをテストリソースに配置して、プロパティをオーバーライドします。 *このファイルは、* *デフォルトのクラスパスと同じ*クラスパス上にある必要があります。
さらに、デフォルトのファイルで指定されたすべてのプロパティキー*を含む必要があります。 したがって、_application.properties_ファイルを_src / test / java / resources_に追加します。
example.firstProperty=file
example.secondProperty=file
ソリューションを利用するテストも追加しましょう。
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestResourcePropertySourceResolverIntegrationTest {
@Autowired private PropertySourceResolver propertySourceResolver;
@Test
public void shouldTestResourceFile_overridePropertyValues() {
String firstProperty = propertySourceResolver.getFirstProperty();
String secondProperty = propertySourceResolver.getSecondProperty();
assertEquals("file", firstProperty);
assertEquals("file", secondProperty);
}
}
このメソッドは、ファイルから複数のプロパティをオーバーライドする場合に非常に効果的です。
また、ファイルに__example.secondProperty ___を入れなかった場合、アプリケーションコンテキストはこのプロパティを検出しません。
5. 春のプロファイル
このセクションでは、Spring Profilesを使用して問題に対処する方法を学習します。 *前の方法とは異なり、* *これはデフォルトファイルとプロファイルファイルのプロパティをマージします*。
最初に、_src / test / java / resources:_に_application **– ** test.properties_ファイルを作成します。
example.firstProperty=profile
その後、_test_プロファイルを使用するテストを作成します。
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class ProfilePropertySourceResolverIntegrationTest {
@Autowired private PropertySourceResolver propertySourceResolver;
@Test
public void shouldProfiledProperty_overridePropertyValues() {
String firstProperty = propertySourceResolver.getFirstProperty();
String secondProperty = propertySourceResolver.getSecondProperty();
assertEquals("profile", firstProperty);
assertEquals("defaultSecond", secondProperty);
}
}
このアプローチにより、デフォルト値とテスト値の両方を使用できます。 したがって、これは、ファイルから複数のプロパティをオーバーライドする必要があるが、デフォルトのプロパティを使用したい場合に最適な方法です。
さらに、https://www.baeldung.com/spring-profiles [_Spring Profiles_]チュートリアルでSpringプロファイルの詳細を確認できます。
6. _ @ SpringBootTest_
プロパティ値をオーバーライドする別の方法は、_ @ SpringBootTest_アノテーションを使用することです。
@RunWith(SpringRunner.class)
@SpringBootTest(properties = { "example.firstProperty=annotation" })
public class SpringBootPropertySourceResolverIntegrationTest {
@Autowired private PropertySourceResolver propertySourceResolver;
@Test
public void shouldSpringBootTestAnnotation_overridePropertyValues() {
String firstProperty = propertySourceResolver.getFirstProperty();
String secondProperty = propertySourceResolver.getSecondProperty();
Assert.assertEquals("annotation", firstProperty);
Assert.assertEquals("defaultSecond", secondProperty);
}
}
*ご覧のとおり、* * _example.firstProperty_はオーバーライドされていますが、_example.secondProperty_はオーバーライドされていません*。 したがって、これはテストの特定のプロパティのみをオーバーライドする必要がある場合に最適なソリューションです。 これは、Spring Bootの使用を必要とする唯一の方法です。
7. TestPropertySourceUtils
このセクションでは、_ApplicationContextInitializerの_TestPropertySourceUtils_クラスを使用してプロパティをオーバーライドする方法を学習します。 _
_TestPropertySourceUtils_には、異なるプロパティ値を定義するために使用できる2つのメソッドが付属しています。
テストで使用する初期化クラスを作成しましょう。
public class PropertyOverrideContextInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
static final String PROPERTY_FIRST_VALUE = "contextClass";
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
configurableApplicationContext, "example.firstProperty=" + PROPERTY_FIRST_VALUE);
TestPropertySourceUtils.addPropertiesFilesToEnvironment(
configurableApplicationContext, "context-override-application.properties");
}
}
次に、_context-override-application.properties_ファイルを_src / test / resources:_に追加します
example.secondProperty=contextFile
最後に、初期化子を使用するテストクラスを作成する必要があります。
@RunWith(SpringRunner.class)
@ContextConfiguration(
initializers = PropertyOverrideContextInitializer.class,
classes = Application.class)
public class ContextPropertySourceResolverIntegrationTest {
@Autowired private PropertySourceResolver propertySourceResolver;
@Test
public void shouldContext_overridePropertyValues() {
final String firstProperty = propertySourceResolver.getFirstProperty();
final String secondProperty = propertySourceResolver.getSecondProperty();
assertEquals(PropertyOverrideContextInitializer.PROPERTY_FIRST_VALUE, firstProperty);
assertEquals("contextFile", secondProperty);
}
}
_example.firstProperty_は、インライン化されたメソッドからオーバーライドされました。
_example.secondProperty_は、2番目のメソッドで特定のファイルからオーバーライドされています。 このアプローチにより、コンテキストを初期化するときに異なるプロパティ値を定義できます。
8. 結論
このチュートリアルでは、テストでプロパティをオーバーライドするために使用できる複数の方法に注目しました。
また、各ソリューションをいつ使用するか、場合によってはそれらを混合する場合も発見しました。
もちろん、https://www.baeldung.com/spring-test-property-source [_ @ TestPropertySource_アノテーション]も自由に使用できます。
いつものように、例のコードはhttps://github.com/eugenp/tutorials/tree/master/testing-modules/spring-testing[GitHubで]から入手できます。