Inject / override a properties value to a Spring B

2020-07-30 02:17发布

问题:

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.