Override default Spring-Boot application.properties settings in Junit Test with dynamic value

前端 未结 4 1712
挽巷
挽巷 2020-12-10 16:56

I want to override properties defined in application.properties in tests, but @TestPropertySource only allows to provide predefined values.

What I need is to start a

4条回答
  •  不知归路
    2020-12-10 17:18

    Thanks to the changes made in Spring Framework 5.2.5, the use of @ContextConfiguration and the ApplicationContextInitializer can be replaced with a static @DynamicPropertySource method that serves the same purpose.

    @SpringBootTest
    @Testcontainers
    class SomeSprintTest {
    
        @Container
        static LocalStackContainer localStack = 
            new LocalStackContainer().withServices(LocalStackContainer.Service.S3);
    
        @DynamicPropertySource
        static void initialize(DynamicPropertyRegistry registry) {
            AwsClientBuilder.EndpointConfiguration endpointConfiguration = 
                localStack.getEndpointConfiguration(LocalStackContainer.Service.S3);
    
            registry.add("cloud.aws.s3.default-endpoint", endpointConfiguration::getServiceEndpoint);
        }
    }
    

提交回复
热议问题