Add the dependency for Testcontainers Spring Config Server in your spring boot application:
testImplementation 'com.frog-development.testcontainers:testcontainers-config-server:2020.0.2'N.B.: The tag will use the Spring Cloud Release tag, to easily understand what version of spring is used.
The auto-configuration will automatically launch 1 container testcontainers-config-server to be used during your integration tests.
Here are the default values embedded by default with the dependency :
testcontainers:
config-server:
enabled: true # Enable/Disable configuration of config server on startup.docker:
image: frogdevelopment/testcontainers-config-server # Docker image to use.tag: Hoxton.SR6 # Docker tag to use.git:
uri: your_own_git_uri # Remote Git repository to use.branch: the_branch_or_tag_I_want_to_use # Branch or tag of the repository to use.clone-on-start: true # Flag to indicate that the repository should be cloned on startup (not on demand). Generally leads to slower startup but faster first query.refresh-rate: 30# Time (in seconds) between refresh of the git repository, defaults to 30 seconds.timeout: 15# Timeout (in seconds) for obtaining HTTP or SSH connection (if applicable), defaults to 5 seconds.server:
port: 8888# Server HTTP port.wait-timeout: 60# Duration (in seconds) of waiting time until container treated as started, defaults to 60 secondshealth-path: /actuator/health # Path to the actuator health's endpoint, default to "/actuator/health".You can override any properties by updating or adding bootstrap-test.yml file in src/test/resources spring project path.
It is also possible to locally enable/disable the run of the testcontainers by setting the property testcontainers.config-server.enabled to false:
@SpringBootTest(properties = {
"testcontainers.config-server.enabled=false",
"spring.cloud.config.enabled=false"
})or
@TestPropertySource(properties = {
"testcontainers.config-server.enabled=false",
"spring.cloud.config.enabled=false"
})As it can take a little more time at the beginning when testing locally, you can disable test containers and use a running locally spring cloud config server.
bootstrap-test.yml
spring:
cloud:
config:
uri: http://localhost:8888label: the_branch_or_tag_I_want_to_usetestcontainers:
config-server:
enabled: falseHere is a docker-compose config file to launch a local config server
docker-compose.yml
spring-config-server:
container_name: spring-config-serverimage: frogdevelopment/testcontainers-config-server:2020.0.2ports:
- 8888:8080environment:
SPRING_CLOUD_CONFIG_SERVER_GIT_URI: your_own_git_uriSPRING_CLOUD_CONFIG_SERVER_GIT_DEFAULT_LABEL: developSPRING_CLOUD_CONFIG_SERVER_GIT_CLONE_ON_START: 'false'- add authentication configuration https://cloud.spring.io/spring-cloud-config/reference/html/#_git_ssh_configuration_using_properties