LoginSignup
1
0

More than 3 years have passed since last update.

spring-bootのDynamicPropertyRegistryでtest時に動的にプロパティ変更

Posted at
src/main/resources/application.properties
sample.value=sample
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

@SpringBootTest
class DynamicPropertySourceTest {

    @DynamicPropertySource
    static void registerProperties(DynamicPropertyRegistry registry) {
        registry.add("sample.value", () -> "dynamic sample value.");
    }

    @Value("${sample.value}")
    String value;

    @Test
    void test() {
        System.out.println(value); //dynamic sample value.と表示
    }

}

これの主な使いどころは、ユニットテストの外部で起動するものに依存する場合に使用する。たとえば、 https://www.baeldung.com/spring-dynamicpropertysource にあるとおりTestcontainersとの組み合わせで用いる。dockerで起動したコンテナからポートとかJDBC URLとかを取得してspring-bootのプロパティとして指定する。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0