testcontainers-java-spring-boot-quickstart icon indicating copy to clipboard operation
testcontainers-java-spring-boot-quickstart copied to clipboard

Spring's @DynamicPropertySource registering config too late for bootstrapping Testcontainer

Open vaughan-naidoo opened this issue 2 years ago • 1 comments

I am setting Spring config by running a HashiCorp Vault container for local development using Testcontainers. Before DynamicPropertySource properties are set, Spring has already configured Vault using defaults and attempts to connect to the Vault instance using these defaults (e.g. Spring configures port 8200 instead of the first mapped port of the Vault Testcontainer).

The result is a connection refused error "Connect to http://localhost:8200 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: no further information". This is due to the config being incorrect and the Vault Testcontainer not running at that time.

I have a configuration class annotated @TestConfiguration and run the application main with this config as documented.

vaughan-naidoo avatar Jul 14 '23 08:07 vaughan-naidoo

I think Spring eagerly try to connect to value to fetch properties. There are similar issues while loading properties from AWS ParameterStore/SecretsManager. One of option is to eagerly starting the Vault container using a static method/block and set Vault config properties as System Properties.

I did something similar to run Spring Cloud AWS application locally using LocalStack. Please take a look at https://youtu.be/OPQEjpX-yio?t=485

sivaprasadreddy avatar Jul 19 '23 11:07 sivaprasadreddy