Inject / override a properties value to a Spring Boot properties file in unit/integration tests phase

会有一股神秘感。 提交于 2019-12-23 03:25:10

问题


Working with Spring Boot and Testcontainers I need a way to dynamically tell the app what is the port in which the testcontainer is listening.

I know that during tests I can tell Spring to use a different properties file:

@TestPropertySource(locations = "classpath:application-integrationtests.yml")

But since the port will be random, I need to programmatically inject the value to the Spring or to the properties file.

I'm not talking about @Value parameter as it will inject to the bean a value from the properties file, because when the app is in test phase, there is no way to know what this value will be.


回答1:


Following @Dirk Deyne great link to an example from testcontainers demo I'm adding here a copy (with small modifications) of Testcontainer's solution to the above question:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class,webEnvironment = 
                           WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = MyIntegrationTest.Initializer.class)
public class MyIntegrationTest {

public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        TestPropertyValues values = TestPropertyValues.of(
                "some.value.1=" + someObject.getSomeValue(),
                "some.value.2=" + someObject.getOtherValue()
        );
        values.applyTo(configurableApplicationContext);
    }
  }
}



回答2:


There's probably a better way, but I've just been using System properties for this.

@SpringBootTest
@DirtiesContext
public class MyTest {
    @BeforeClass
    public static void setUpEnvironment() {
        System.setProperty("kafka.bootstrap.servers", testKafka.getServers(); 
    }
    ...
}



回答3:


Hard to write a correct answer as you don't show the code where you use the Testcontainers. But from the documentation:

The class rule provides methods for discovering how your tests can interact with the containers:

getContainerIpAddress() returns the IP address where the container is listening getMappedPort(...) returns the Docker mapped port for a port that has been exposed on the container

For example, with the Redis example above, the following will allow your tests to access the Redis service:

String redisUrl = redis.getContainerIpAddress() + ":" + redis.getMappedPort(6379);

So you should be able to easily access this information.



来源:https://stackoverflow.com/questions/50894302/inject-override-a-properties-value-to-a-spring-boot-properties-file-in-unit-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!