Library containing Spring Boot starters which manage lifecycle (start/stop) of testcontainers.
Usual use cases include:
- tests (container is started during test spring context initialization and stopped during context destruction)
- local development (e.g. to remove manual setup of local DB)
For changes check the changelog.
This library tries to reuse existing Spring Boot configuration classes and enhance their behaviour by performing some extra steps around them.
Generally, in cases where port placeholders are used (<port>
), the library will make sure that the appropriate Docker container is started on
a randomly selected open port and that the selected value will be used by the configuration in the runtime.
You can use a concrete value instead of the placeholder - in that case the library will attempt to start the container on the specified port.
If reuse is enabled this project automatically marks all created containers for reuse.
Disclaimer: by using this testcontainer you accept the EULA for mssql docker image as required here.
Include the dependency:
<dependency>
<groupId>com.infobip</groupId>
<artifactId>infobip-mssql-testcontainers-spring-boot-starter</artifactId>
<version>${infobip-mssql-testcontainers-spring-boot-starter.version}</version>
<scope>test</scope>
</dependency>
jTDS:
spring:
datasource:
url: jdbc:jtds:sqlserver://<host>:<port>/FooBarDb
Microsoft driver:
spring:
datasource:
url: jdbc:sqlserver://<host>:<port>;database=FooBarDb
Logical database is automatically created.
Container IP address is resolved based on running host, meaning on local machine <host>
will resolve to localhost
while inside Docker placeholder will resolve to containerIp
.
When <port>
placeholder is used, container will be mapped on random port and automatically substituted.
Add the following profile:
<profiles>
<profile>
<id>development</id>
<dependencies>
<dependency>
<groupId>com.infobip</groupId>
<artifactId>infobip-mssql-testcontainers-spring-boot-starter</artifactId>
<version>${infobip-mssql-testcontainers-spring-boot-starter.version}</version>
</dependency>
</dependencies>
</profile>
</profiles>
Before starting the application locally, activate development profile:
and update your local configuration (e.g. application-development.yaml):
jTDS:
spring:
datasource:
url: jdbc:jtds:sqlserver://<host>:<port>/FooBarDb_test_${user.name}
Microsoft driver:
spring:
datasource:
url: jdbc:sqlserver://<host>:<port>;database=FooBarDb_test_${user.name}
To change the docker image used simply add the following property (e.g. in yaml):
testcontainers.mssql.docker.image: mcr.microsoft.com/mssql/server:2017-CU12
To add an SQL script with which the container will be initialized with add the following property (e.g. in yaml):
testcontainers.mssql.init-script: db/init-script.sql
Include the dependency:
<dependency>
<groupId>com.infobip</groupId>
<artifactId>infobip-mysql-testcontainers-spring-boot-starter</artifactId>
<version>${infobip-mysql-testcontainers-spring-boot-starter.version}</version>
<scope>test</scope>
</dependency>
Configuration:
spring:
datasource:
url: jdbc:mysql://<host>:<port>/FooBarDb
username: test
password: test
Logical database is automatically created.
Container IP address is resolved based on running host, meaning on local machine <host>
will resolve to localhost
while inside Docker placeholder will resolve to containerIp
.
When <port>
placeholder is used, container will be mapped on random port and automatically substituted.
Add the following profile:
<profiles>
<profile>
<id>development</id>
<dependencies>
<dependency>
<groupId>com.infobip</groupId>
<artifactId>infobip-mysql-testcontainers-spring-boot-starter</artifactId>
<version>${infobip-mysql-testcontainers-spring-boot-starter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>
Before starting the application locally, activate development profile:
and update your local configuration (e.g. application-development.yaml):
spring:
datasource:
url: jdbc:mysql://<host>:<port>/FooBarDb_test_${user.name}
username: test
password: test
To change the docker image used simply add the following property (e.g. in yaml):
testcontainers.mysql.docker.image: mysql:5.7.34
To add an SQL script with which the container will be initialized with add the following property (e.g. in yaml):
testcontainers.mysql.init-script: db/init-script.sql
Include the dependency:
<dependency>
<groupId>com.infobip</groupId>
<artifactId>infobip-postgresql-testcontainers-spring-boot-starter</artifactId>
<version>${infobip-postgresql-testcontainers-spring-boot-starter.version}</version>
<scope>test</scope>
</dependency>
Configuration:
spring:
datasource:
url: jdbc:postgresql://<host>:<port>/FooBarDb
username: test
password: test
Logical database is automatically created.
Container IP address is resolved based on running host, meaning on local machine <host>
will resolve to localhost
while inside Docker placeholder will resolve to containerIp
.
When <port>
placeholder is used, container will be mapped on random port and automatically substituted.
Add the following profile:
<profiles>
<profile>
<id>development</id>
<dependencies>
<dependency>
<groupId>com.infobip</groupId>
<artifactId>infobip-postgresql-testcontainers-spring-boot-starter</artifactId>
<version>${infobip-postgresql-testcontainers-spring-boot-starter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>
Before starting the application locally, activate development profile:
and update your local configuration (e.g. application-development.yaml):
spring:
datasource:
url: jdbc:postgresql://<host>:<port>/FooBarDb_test_${user.name}
username: test
password: test
To change the docker image used simply add the following property (e.g. in yaml):
testcontainers.postgresql.docker.image: postgres:15
To add an SQL script with which the container will be initialized with add the following property (e.g. in yaml):
testcontainers.postgresql.init-script: db/init-script.sql
Include the dependency:
<dependency>
<groupId>com.infobip</groupId>
<artifactId>infobip-redis-testcontainers-spring-boot-starter</artifactId>
<version>${infobip-redis-testcontainers-spring-boot-starter.version}</version>
<scope>test</scope>
</dependency>
Configuration:
spring:
redis:
url: redis://<host>:<port>
Container IP address is resolved based on running host, meaning on local machine <host>
will resolve to localhost
while inside Docker placeholder will resolve to containerIp
.
When <port>
placeholder is used, container will be mapped on random port and automatically substituted.
Add the following profile:
<profiles>
<profile>
<id>development</id>
<dependencies>
<dependency>
<groupId>com.infobip</groupId>
<artifactId>infobip-redis-testcontainers-spring-boot-starter</artifactId>
<version>${infobip-redis-testcontainers-spring-boot-starter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>
Before starting the application locally, activate development profile:
and update your local configuration (e.g. application-development.yaml):
spring:
redis:
url: redis://<host>:<port>
To change the docker image used simply add the following property (e.g. in yaml):
testcontainers.redis.docker.image: redis:5.0.7-alpine
Format: <topicName>:<numPartitions>:<replicationFactor>
testcontainers.kafka.topics: test-topic:1:1, test-topic-2:1:1
Include the dependency:
<dependency>
<groupId>com.infobip</groupId>
<artifactId>infobip-kafka-testcontainers-spring-boot-starter</artifactId>
<version>${infobip-kafka-testcontainers-spring-boot-starter.version}</version>
<scope>test</scope>
</dependency>
Configuration:
spring:
kafka:
bootstrap-servers: <host>:<port>
Logical database is automatically created.
Container IP address is resolved based on running host, meaning on local machine <host>
will resolve to localhost
while inside Docker placeholder will resolve to containerIp
.
When <port>
placeholder is used, container will be mapped on random port and automatically substituted.
Add the following profile:
<profiles>
<profile>
<id>development</id>
<dependencies>
<dependency>
<groupId>com.infobip</groupId>
<artifactId>infobip-kafka-testcontainers-spring-boot-starter</artifactId>
<version>${infobip-kafka-testcontainers-spring-boot-starter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>
Before starting the application locally, activate development profile:
and update your local configuration (e.g. application-development.yaml):
spring:
kafka:
bootstrap-servers: <host>:<port>
To change the docker image version used simply add the following property (e.g. in yaml):
testcontainers.kafka.docker.image.version: 2.1.0
Include the dependency:
<dependency>
<groupId>com.infobip</groupId>
<artifactId>infobip-rabbitmq-testcontainers-spring-boot-starter</artifactId>
<version>${infobip-rabbitmq-testcontainers-spring-boot-starter.version}</version>
<scope>test</scope>
</dependency>
To configure RabbitMq in tests you need to create it's configuration for example:
@Configuration
@Profile({"test", "development"})
public class RabbitConfigTestEnv {
@Bean
public Queue testQueue() {
return QueueBuilder.durable("test.queue").build();
}
@Bean
public TopicExchange testExchange() {
return new TopicExchange("test.exchange");
}
@Bean
public Binding bindToTestExchange() {
return bind(testQueue()).to(testExchange()).with("test.key.#");
}
}
This class should live inside test files and there you can create queues, exchanges and key routing bindings or receivers. In this example method:
testQueue
creates queue with nametest.queue
testExchange
creates exchange with nametest.exchange
bindToTestExchange
tells Rabbit to send any message sent to test exchange, with key of valuetest.key.#
to our test queue
Important: Queues are declared in Rabbit only after some message is sent to the queue.
If you log into docker
and try to find queue, it won't be listed if no message was sent to it.
Add the following profile:
<profiles>
<profile>
<id>development</id>
<dependencies>
<dependency>
<groupId>com.infobip</groupId>
<artifactId>infobip-rabbitmq-testcontainers-spring-boot-starter</artifactId>
<version>${infobip-rabbitmq-testcontainers-spring-boot-starter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>
Before starting the application locally, activate development profile:
To change the docker image used simply add the following property (e.g. in yaml):
testcontainers.rabbit.docker.image: rabbitmq:3.6.14-alpine
Include the dependency:
<dependency>
<groupId>com.infobip</groupId>
<artifactId>infobip-clickhouse-testcontainers-spring-boot-starter</artifactId>
<version>${infobip-clickhouse-testcontainers-spring-boot-starter.version}</version>
<scope>test</scope>
</dependency>
Add the following profile:
<profiles>
<profile>
<id>development</id>
<dependencies>
<dependency>
<groupId>com.infobip</groupId>
<artifactId>infobip-clickhouse-testcontainers-spring-boot-starter</artifactId>
<version>${infobip-clickhouse-testcontainers-spring-boot-starter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>
Before starting the application locally, activate development profile:
and update your local configuration (e.g. application-development.yaml):
spring:
datasource:
jdbc-url: <host>:<port>
In case your datasource configuration is different from default one you can provide custom configuration property path
testcontainers.clickhouse.custom-path: "spring.datasource.clickhouse"
in this case your configuration would look like this
spring:
datasource:
clickhouse:
jdbc-url: <host>:<port>
To change the docker image used simply add the following property (e.g. in yaml):
testcontainers.clickhouse.docker.image: clickhouse:latest
To add an SQL script with which the container will be initialized with add the following property (e.g. in yaml):
testcontainers.clickhouse.init-script: db/init-script.sql
If you have an idea for a new feature or want to report a bug please use the issue tracker.
Pull requests are welcome!
This library is licensed under the Apache License, Version 2.0.