### Example Spring Boot Test Configuration (YAML)
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-postgresql/README.adoc
Configure embedded PostgreSQL and Spring Data Source properties for testing using YAML files. This example shows how to set the Docker image, wait timeout, and mount volumes for persistence and configuration.
```yaml
embedded:
postgresql:
enabled: true
docker-image: 'postgres:16-alpine'
wait-timeout-in-seconds: 40
command: '/opt/bitnami/scripts/postgresql/run.sh'
startupLogCheckRegex: '.*database system is ready to accept connections.'
mountVolumes:
-
hostPath: 'pgdata'
containerPath: '/bitnami/postgresql'
mode: READ_WRITE
-
hostPath: 'src/main/resources/my-postgresql.conf'
containerPath: '/bitnami/postgresql/conf/postgresql.conf'
mode: READ_ONLY
```
```yaml
spring:
datasource:
url: "jdbc:postgresql://${embedded.postgresql.host}:${embedded.postgresql.port}/${embedded.postgresql.schema}"
username: ${embedded.postgresql.user}
password: ${embedded.postgresql.password}
```
--------------------------------
### Keycloak Configuration Example
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-keycloak/README.adoc
Configure the Keycloak auth server URL in your test application.yaml by referencing the embedded Keycloak properties.
```yaml
keycloak:
auth-server-url: ${embedded.keycloak.auth-server-url}
```
--------------------------------
### Example: Native Kafka with Toxiproxy in Test
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-native-kafka/README.adoc
Demonstrates how to enable Toxiproxy for Kafka in a Spring Boot test and inject the ToxiproxyClientProxy. Use this to simulate network latency.
```java
@SpringBootTest
@TestPropertySource(properties = {
"embedded.toxiproxy.proxies.kafka.enabled=true"
})
class NativeKafkaWithToxiproxyTest {
@Value("${embedded.kafka.toxiproxy.bootstrapServers}")
String proxiedBootstrapServers;
@Autowired(required = false)
@Qualifier("nativeKafkaContainerProxy")
ToxiproxyClientProxy proxy;
@Test
void testWithLatency() {
// Add 500ms latency
proxy.toxics()
.latency("latency", ToxicDirection.DOWNSTREAM, 500);
// Test Kafka with network latency
}
}
```
--------------------------------
### Embedded Native Kafka Configuration Example
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-native-kafka/README.adoc
Configure topics to create and enable filesystem binding for the embedded native Kafka container via bootstrap.properties.
```properties
embedded.kafka.topicsToCreate=topic1,topic2
embedded.kafka.fileSystemBind.enabled=true
```
--------------------------------
### Spring Cloud Vault Configuration Example
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-vault/README.adoc
Example of how to configure Spring Cloud Vault to use the embedded Vault service in your test environment.
```APIDOC
## Spring Cloud Vault Configuration Example
To auto-configure `spring-cloud-vault-config` use these properties in your test `application.yaml`:
./src/test/resources/application.yaml
[source,yaml]
----
spring.cloud.vault:
scheme: http
host: ${embedded.vault.host}
port: ${embedded.vault.port}
token: ${embedded.vault.token}
kv:
enabled: true
----
```
--------------------------------
### Create Vanilla ToxiProxy Instance
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-toxiproxy/README.adoc
Use ToxiProxyClient to create a proxy for your service. Ensure the ToxiProxy available port is within the specified range. This example also demonstrates adding a latency toxic.
```java
@Value("${embedded.toxiproxy.host}")
String host;
@Value("${embedded.toxiproxy.controlPort}")
int controlPort;
ToxiproxyClient toxiproxyClient = new ToxiproxyClient(host, controlPort);
Proxy proxy = toxiproxyClient.createProxy(
"my-service-proxy", // Name of the proxy, any that you like
// NOTE: {toxi-proxy-avail-port} must be in the range [8666; 8666 + 31] (these ports are exposed by default).
"0.0.0.0:{toxi-proxy-avail-port}", // Toxiproxy will listen on this address.
"{my-service-host}:{my-service-port}"); // Your service is exposed at this address
proxy.toxics()
.latency("latency", ToxicDirection.DOWNSTREAM, 1_100)
.setJitter(100);
```
--------------------------------
### Configure Memsql Container Start Behavior
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
This table illustrates how the 'embedded.containers.enabled' and 'embedded.memsql.enabled' properties affect whether the Memsql container starts. The Memsql container starts only when both properties are true or when both are missing (defaulting to true).
```properties
embedded.containers.enabled=false
embedded.memsql.enabled=true
```
```properties
embedded.containers.enabled=true
embedded.memsql.enabled=false
```
```properties
embedded.containers.enabled=true
embedded.memsql.enabled=true
```
```properties
embedded.memsql.enabled is missing
```
--------------------------------
### Pulsar Client Initialization with Broker URL
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-pulsar/README.adoc
Example of creating a PulsarClient using the broker URL provided by the embedded Pulsar environment. Ensure the embedded-pulsar module is configured and enabled.
```java
PulsarClient pulsarClient = PulsarClient.builder()
.serviceUrl(environment.getProperty("embedded.pulsar.brokerUrl"))
.build();
```
--------------------------------
### Add Spring Boot Starter and Spring Cloud Starter Bootstrap for Test Scope
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
Configure dependencies for testing when not using Spring Cloud in production. This setup includes the Spring Boot starter and scopes the Spring Cloud starter bootstrap dependency to 'test'.
```xml
...
org.springframework.boot
spring-boot-starter
[pick version]
org.springframework.cloud
spring-cloud-starter-bootstrap
[pick version]
test
...
```
--------------------------------
### Set Container Wait Timeout
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
Configure the maximum time in seconds to wait for a container to start. The default value is 60 seconds.
```yaml
embedded.redis.waitTimeoutInSeconds=120
```
--------------------------------
### InfluxDB Test Configuration Example
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-influxdb/README.adoc
Configure InfluxDB connection properties in your test application.yaml using properties provided by the embedded InfluxDB module.
```yaml
influxdb:
url: http://${embedded.influxdb.host}:${embedded.influxdb.port}
user: ${embedded.influxdb.user}
password: ${embedded.influxdb.password}
database: ${embedded.influxdb.database}
```
--------------------------------
### Example Test Class with Embedded Selenium
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-selenium/README.adoc
An example test class demonstrating how to use embedded Selenium with Testcontainers. It injects the WebDriver and accesses the application under test.
```java
import org.openqa.selenium.WebDriver;
@RunWith(SpringRunner.class)
@SpringBootTest(
classes = SpringBootWebApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)
@Import(TestLoginPage.LocalTestConfiguration.class)
public class TestLoginPage {
@LocalServerPort
private int port;
@DockerHostname
private String hostname;
@Autowired
private BrowserWebDriverContainer webDriverContainer;
@Test
public void test1() {
RemoteWebDriver webDriver = webDriverContainer.getWebDriver();
webDriver.get("https://" + hostname + ":" + port);
assertThat(webDriver.getPageSource()).contains("helloworld");
}
@TestConfiguration
static class LocalTestConfiguration {
@Bean
public FirefoxOptions firefoxOptions(SeleniumProperties properties) {
FirefoxOptions options = new FirefoxOptions();
properties.apply(options);
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
options.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
return options;
}
}
}
```
--------------------------------
### Configure Embedded Google Storage Buckets
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-google-storage/README.adoc
Use this property to define the names of buckets that should be created when the embedded Google Storage container starts.
```properties
embedded.google.storage.buckets[0].name=my-bucket0
embedded.google.storage.buckets[1].name=my-bucket1
```
--------------------------------
### Java Usage Example for Embedded Native Kafka
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-native-kafka/README.adoc
Demonstrates sending a message using KafkaTemplate in a Spring Boot test. Ensure your test class is annotated with @SpringBootTest.
```java
@SpringBootTest
class EmbeddedNativeKafkaTest {
@Autowired
private KafkaTemplate kafkaTemplate;
@Value("${embedded.kafka.bootstrapServers}")
private String bootstrapServers;
@Test
void testKafkaMessageSendAndReceive() {
// Your test logic here
kafkaTemplate.send("test-topic", "test-message");
// Assert message consumption
// ...
}
}
```
--------------------------------
### Application Configuration for Embedded Selenium
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-selenium/README.adoc
Configure embedded Selenium behavior using properties in application.yaml. This example sets the browser to Chromium and enables certificate error ignoring.
```yaml
embedded:
selenium:
enabled: true
browser: CHROMIUM
arguments:
- ignore-certificate-errors
```
--------------------------------
### Custom Firefox Options Configuration
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-selenium/README.adoc
Define custom FirefoxOptions in a Spring @TestConfiguration. This example applies properties and adds specific arguments and capabilities.
```java
@TestConfiguration
static class LocalTestConfiguration {
@Bean
public FirefoxOptions firefoxOptions(SeleniumProperties properties) {
FirefoxOptions options = new FirefoxOptions();
properties.apply(options);
options.addArguments("hello-world");
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, false);
options.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, false);
return options;
}
}
```
--------------------------------
### Enable Specific Embedded Container
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
Set this property to true to enable a specific embedded container module to be started on application startup. The default value is true.
```yaml
embedded.redis.enabled=true
```
--------------------------------
### Enable Embedded Containers
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
Set this property to true to enable all embedded containers. The default value is true. If disabled, no embedded containers will start, regardless of individual container enablement.
```properties
embedded.containers.enabled=true
```
--------------------------------
### Set Custom Container Command
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
Provide a list of keywords to construct the command for container startup. Be cautious, as some modules have default commands, and resetting this may lead to incorrect container behavior.
```yaml
embedded.redis.command=["--appendonly", "yes"]
```
--------------------------------
### Configure spring-data-aerospike-starter (With ToxiProxy)
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-aerospike/README.adoc
Configure bootstrap.properties to enable ToxiProxy and connect the spring-data-aerospike-starter through it to the embedded Aerospike instance.
```properties
embedded.toxiproxy.proxies.aerospike.enabled=true
spring.aerospike.hosts=${embedded.aerospike.toxiproxy.host}:${embedded.aerospike.toxiproxy.port}
spring.data.aerospike.namespace=${embedded.aerospike.namespace}
```
--------------------------------
### Configure spring-data-aerospike-starter (No ToxiProxy)
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-aerospike/README.adoc
Set properties in bootstrap.properties to configure the spring-data-aerospike-starter to connect directly to the embedded Aerospike instance.
```properties
spring.aerospike.hosts=${embedded.aerospike.host}:${embedded.aerospike.port}
spring.data.aerospike.namespace=${embedded.aerospike.namespace}
```
--------------------------------
### Auto-configure Spring Cloud GCP Pub/Sub with Embedded Emulator
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-google-pubsub/README.adoc
Configure application.yaml to use the embedded Google Pub/Sub emulator's host and port for spring-cloud-gcp-starter-pubsub.
```yaml
spring.cloud.gcp:
project-id: ${embedded.google.pubsub.project-id}
pubsub.emulatorHost: ${embedded.google.pubsub.host}:${embedded.google.pubsub.port}
```
--------------------------------
### Application Kafka Configuration
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-native-kafka/README.adoc
Set up Kafka consumer and producer configurations in your application properties, referencing the embedded Kafka bootstrap servers.
```properties
# Kafka configuration
bootstrap.servers=${embedded.kafka.bootstrapServers}
# Consumer configuration
spring.kafka.consumer.bootstrap-servers=${embedded.kafka.bootstrapServers}
spring.kafka.consumer.group-id=test-group
# Producer configuration
spring.kafka.producer.bootstrap-servers=${embedded.kafka.bootstrapServers}
```
--------------------------------
### Create Aerospike Client via ToxiProxy
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-aerospike/README.adoc
Instantiate an AerospikeClient connected to the ToxiProxy instance, which then forwards traffic to the embedded Aerospike server.
```java
@Bean(destroyMethod = "close")
public AerospikeClient aerospikeToxicClient(@Value("${embedded.aerospike.toxiproxy.host}") String host,
@Value("${embedded.aerospike.toxiproxy.port}") int port) {
ClientPolicy clientPolicy = new ClientPolicy();
return new AerospikeClient(clientPolicy, host, port);
}
```
--------------------------------
### Add Spring Cloud Starter Bootstrap Dependency
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
Include this dependency if you are using Spring Boot 2.4 or later and need Spring Cloud for your tests.
```xml
...
org.springframework.cloud
spring-cloud-starter-bootstrap
[pick version]
...
```
--------------------------------
### Create Vanilla Aerospike Client
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-aerospike/README.adoc
Instantiate an AerospikeClient directly connected to the embedded Aerospike server using properties provided by the test container.
```java
@Bean(destroyMethod = "close")
public AerospikeClient aerospikeClient(@Value("${embedded.aerospike.host}") String host,
@Value("${embedded.aerospike.port}") int port) {
ClientPolicy clientPolicy = new ClientPolicy();
return new AerospikeClient(clientPolicy, host, port);
}
```
--------------------------------
### Configure Embedded Kafka Topics to Create
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
Specify topics to be created automatically by the embedded Kafka instance in your bootstrap properties.
```properties
embedded.kafka.topicsToCreate=some_topic
```
--------------------------------
### Configure and Use WireMock in Tests
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-wiremock/README.adoc
Inject the WireMock host and port properties to configure the client and define stubs.
```java
class SomeTest {
//...
@Value("${embedded.wiremock.host}")
String wiremockHost;
@Value("${embedded.wiremock.port}")
int wiremockPort;
@BeforeEach
void setUp() {
WireMock.configureFor(wiremockHost, wiremockPort);
}
@Test
void doTest() {
// configure stub
stubFor(get("/say-hello")
.willReturn(ok("Hello world!")));
// test something
}
//...
}
```
--------------------------------
### Configuration Properties
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-keydb/README.adoc
Configure the embedded KeyDB behavior using properties in `bootstrap.properties` or `application.properties`.
```APIDOC
## Configuration Properties
### Description
Configure the embedded KeyDB behavior using properties in `bootstrap.properties` or `application.properties`.
### Properties
#### Consumes (via `bootstrap.properties`)
* `embedded.keydb.enabled` (true|false, default is true)
* `embedded.keydb.reuseContainer` (true|false, default is false)
* `embedded.keydb.dockerImage` (default is 'eqalpha/keydb:alpine_x86_64_v6.3.3')
* Image versions on https://hub.docker.com/r/eqalpha/keydb/tags[dockerhub]
* `embedded.keydb.waitTimeoutInSeconds` (default is 60 seconds)
* `embedded.keydb.clustered` (true|false, default is false)
* If `true` KeyDB is started in cluster mode
* `embedded.keydb.requirepass` (true|false, default is true)
* `embedded.toxiproxy.proxies.keydb.enabled` Enables both creation of the container with ToxiProxy TCP proxy and a proxy to the `embedded-keydb` container.
#### Produces
* `embedded.keydb.host`
* `embedded.keydb.port`
* `embedded.keydb.user`
* `embedded.keydb.password`
* `embedded.keydb.toxiproxy.host`
* `embedded.keydb.toxiproxy.port`
* `embedded.keydb.networkAlias`
* Bean `ToxiproxyClientProxy keyDbContainerProxy`
```
--------------------------------
### Add WireMock Client Dependency
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-wiremock/README.adoc
Include the standard WireMock client library to configure stubs within your tests.
```xml
com.github.tomakehurst
wiremock
test
```
--------------------------------
### Explicitly Configure Lettuce Client
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-redis/README.adoc
Configure Spring Boot to use the Lettuce Redis client by setting this property in bootstrap.properties.
```properties
spring.data.redis.client-type=lettuce
```
--------------------------------
### Enable Linux Capabilities for Container
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
Configure the Linux capabilities to be enabled for the container. Some modules have default capabilities like NET_ADMIN. You can disable all capabilities by providing an empty value. Refer to the provided link for available capabilities.
```yaml
embedded.aerospike.capabilities=["NET_ADMIN", "SYS_TIME"]
```
--------------------------------
### Enable ToxiProxy for Aerospike
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-aerospike/README.adoc
Configure bootstrap.properties to enable ToxiProxy for the Aerospike container, allowing network condition simulation.
```properties
embedded.toxiproxy.proxies.aerospike.enabled=true
```
--------------------------------
### Configure Embedded Google Pub/Sub Topics and Subscriptions
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-google-pubsub/README.adoc
Define topics, subscriptions, and their configurations in bootstrap.yaml. Supports message ordering and dead-letter queues.
```yaml
embedded.google.pubsub:
topics-and-subscriptions:
- topic: topic0
subscription: subscription0
- topic: topic1
subscription: subscription1
enable-message-ordering: true
dead-letter:
topic: topic0
max-attempts: 10
- topic: topic2
subscription: subscription2
filter: 'attributes.eventType = "ORDER_CREATED"'
```
--------------------------------
### Enable ToxiProxy for NATS
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-nats/README.adoc
Configure bootstrap.properties to enable ToxiProxy for the NATS container. This allows for network manipulation.
```properties
embedded.toxiproxy.proxies.nats.enabled=true
```
--------------------------------
### Enable Toxiproxy for Native Kafka
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-native-kafka/README.adoc
Configure Toxiproxy to enable network condition simulation for the native Kafka container. This property should be set in your bootstrap.properties.
```properties
embedded.toxiproxy.proxies.kafka.enabled=true
```
--------------------------------
### Mount TmpFS for Container Directories
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
Specify a list of container directories to be replaced with tmpfs mounts, along with their mount options. This is useful for temporary storage within the container.
```yaml
embedded:
mariadb:
tmp-fs:
mounts:
- folder: /var/lib/mysql
options: rw
```
--------------------------------
### Configure Consul in application.yml
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-consul/README.adoc
Use these properties to enable the Consul container and specify a custom configuration file.
```yaml
embedded:
containers:
enabled: true
consul:
enabled: true
# file to be mounted in docker as '/consul/config/test-acl.hcl'
# path relative from the resources directory (usually 'src/test/resources')
configurationFile: consul/test-acl.hcl
```
--------------------------------
### Configure Embedded OpenSearch Client
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-opensearch/README.adoc
Set the OpenSearch URIs in your test application.properties to connect to the autoconfigured embedded OpenSearch client.
```properties
opensearch.uris=http://${embedded.opensearch.host}:${embedded.opensearch.httpPort}
```
--------------------------------
### Enable RabbitMQ Streams Plugin and Additional Port
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-rabbitmq/README.adoc
Configure bootstrap.properties to enable the RabbitMQ streams plugin and expose the default Streams protocol port (5552).
```properties
embedded.rabbitmq.enabled-plugins[0]=rabbitmq_stream
embedded.rabbitmq.additionalPorts[0]=5552
```
--------------------------------
### Bootstrap Configuration for Embedded Git
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-git/README.adoc
Configure the embedded Git server using bootstrap.yml. Specify the path to your local Git repositories and optionally the path to authorized keys for SSH authentication.
```yaml
embedded:
git:
path-to-repositories: path/to/repositories
path-to-authorized-keys: path/to/keys/test_key.pub #remove if you need only username/password auth
```
--------------------------------
### Include Local Files in Container
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
Specify a list of local files to be included in the container. Each entry requires 'classpathResource' (path to the local file) and 'containerPath' (destination path within the container).
```yaml
embedded.redis.filesToInclude:
- classpathResource: "/my_local_file.txt"
containerPath: "/etc/path_in_container.txt"
```
--------------------------------
### Spring Boot Application Configuration for Azure Blob and Queue Storage
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-azurite/README.adoc
Configure your application.yaml to use the properties provided by embedded-azurite for Azure storage endpoints. This is useful when integrating with spring-cloud-azure-starter-storage-blob and spring-cloud-azure-starter-storage-queue.
```yaml
spring:
cloud:
azure:
storage:
blob:
account-name: ${embedded.azurite.account-name}
account-key: ${embedded.azurite.account-key}
endpoint: ${embedded.azurite.blob-endpoint}
queue:
account-name: ${embedded.azurite.account-name}
account-key: ${embedded.azurite.account-key}
endpoint: ${embedded.azurite.queue-endpoint}
```
--------------------------------
### Configure Embedded Kafka Topics
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-kafka/README.adoc
Define topics to be created and which ones should be secured using ACLs via bootstrap.properties. This configuration is applied to the embedded Kafka instance.
```properties
embedded.kafka.topicsToCreate=nonSecureTopic,secureTopic
embedded.kafka.secureTopics=secureTopic
```
--------------------------------
### Test Aerospike Client with ToxiProxy Latency
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-aerospike/README.adoc
Demonstrates testing Aerospike client resiliency by introducing latency through ToxiProxy and verifying timeout exceptions.
```java
@Value("${embedded.aerospike.namespace}")
String namespace;
@Autowired
ToxiproxyClientProxy aerospikeContainerProxy;
@Qualifier("aerospikeToxicClient")
@Autowired
AerospikeClient aerospikeToxicClient;
@Test
void addsLatency() throws Exception {
Policy policy = new Policy();
policy.setTimeout(200);
Key key = new Key(namespace, "any", "any");
Record record = aerospikeToxicClient.get(policy, key);
aerospikeContainerProxy.toxics()
.latency("latency", ToxicDirection.DOWNSTREAM, 1_100)
.setJitter(100);
assertThatThrownBy(() -> aerospikeToxicClient.get(policy, key))
.isInstanceOf(AerospikeException.Timeout.class);
aerospikeContainerProxy.toxics()
.get("latency").remove();
record = aerospikeToxicClient.get(policy, key);
}
```
--------------------------------
### Configure Embedded K3s Client with Official Java Client
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-k3s/README.adoc
Configure the Kubernetes client using the official Java client library. Ensure the `embedded.k3s.kubeconfig` property is set.
```java
@Bean(destroyMethod = "close")
public ApiClient kubernetesClient(@Value("${embedded.k3s.kubeconfig}") String kubeconfig) {
return Config.fromConfig(new StringReader(kubeConfigYaml));
}
```
--------------------------------
### Enable and Configure Mailhog in Test Environment
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-mailhog/README.adoc
Use bootstrap-test.yml to enable Embedded Mailhog and specify a custom Docker image for testing.
```yaml
embedded:
mailhog:
enabled: true
docker-image: 'mailhog/mailhog:v1.0.1'
```
--------------------------------
### Set Environment Variables for Container
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
Define a key-value map of additional environment variables to be set for the container. The key is the variable name, and the value is its actual value.
```yaml
embedded.redis.env.REDIS_PORT=6379
```
--------------------------------
### Mount Host Volumes to Container
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
Define a list of host volumes to be mounted into the container, persisting data between restarts. Each entry requires 'hostPath' (local path), 'containerPath' (path in container), and optionally 'mode' (READ_ONLY or READ_WRITE).
```yaml
embedded.postgresql.mountVolumes:
- hostPath: "pgdata"
containerPath: "/var/lib/postgresql/data"
mode: READ_WRITE
```
--------------------------------
### Configure Embedded K3s Client with Fabric8 Java Client
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-k3s/README.adoc
Configure the Kubernetes client using the Fabric8 Java client library. Ensure the `embedded.k3s.kubeconfig` property is set.
```java
@Bean(destroyMethod = "close")
public KubernetesClient kubernetesClient(@Value("${embedded.k3s.kubeconfig}") String kubeconfig) {
return new KubernetesClientBuilder().withConfig(Config.fromKubeconfig(kubeconfig)).build();
}
```
--------------------------------
### Configuration Properties
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-vault/README.adoc
Configure the embedded Vault service using properties in your bootstrap.properties or application.yaml file.
```APIDOC
## Configuration Properties
### Consumes (via `bootstrap.properties`)
* `embedded.vault.enabled` (true|false, default is true)
* `embedded.vault.reuseContainer` (true|false, default is false)
* `embedded.vault.dockerImage` (default is 'vault:1.13.3')
** Image versions on https://hub.docker.com/_/vault?tab=tags[dockerhub]
* `embedded.vault.host` (default is 'localhost')
* `embedded.vault.port` (int, default is 8200)
* `embedded.vault.token` (default is '00000000-0000-0000-0000-000000000000')
* `embedded.vault.path` (default is 'secret/application')
* `embedded.vault.secrets` (Map, default is empty)
* `embedded.vault.cas-enabled` (true|false, default is false, enables Check and Set operations for mount)
* `embedded.vault.cas-enabled-for-sub-paths` (enables cas for specified, example: sub-paths sub-path1, sub-path2)
* `embedded.toxiproxy.proxies.vault.enabled` Enables both creation of the container with ToxiProxy TCP proxy and a proxy to the `embedded-vault` container.
```
--------------------------------
### Set Labels for Container
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
Define a key-value map of additional labels to be applied to the container. The key is the label name, and the value is its actual value.
```yaml
embedded.redis.label.environment=test
```
--------------------------------
### Bootstrap Properties for Embedded PostgreSQL
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-postgresql/README.adoc
Configure embedded PostgreSQL settings via bootstrap.properties. Key properties include enabling the container, reusing it, specifying the Docker image, and setting timeouts.
```properties
embedded.postgresql.enabled=true
embedded.postgresql.reuseContainer=false
embedded.postgresql.dockerImage=postgres:18-alpine
embedded.postgresql.waitTimeoutInSeconds=60
embedded.postgresql.database=
embedded.postgresql.user=
embedded.postgresql.password=
embedded.postgresql.initScriptPath=
embedded.postgresql.startupLogCheckRegex=
embedded.postgresql.mountVolumes=[]
toxiproxy.postgresql.enabled=true
```
--------------------------------
### Maven Dependency
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-keydb/README.adoc
Add this dependency to your project's pom.xml to enable embedded KeyDB support.
```APIDOC
## Maven Dependency
### Description
Add this dependency to your project's `pom.xml` to enable embedded KeyDB support.
### File
`pom.xml`
### Code
```xml
com.playtika.testcontainers
embedded-keydb
test
```
```
--------------------------------
### Add Maven Dependency for Embedded Google Pub/Sub
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-google-pubsub/README.adoc
Include this dependency in your pom.xml for test scope to enable embedded Google Pub/Sub.
```xml
com.playtika.testcontainers
embedded-google-pubsub
test
```
--------------------------------
### Configure Kafka Bootstrap Servers
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
Use the embedded Kafka broker list property in your application properties to configure the Kafka bootstrap servers for your application during tests.
```properties
spring.kafka.bootstrap-servers=${embedded.kafka.brokerList}
```
--------------------------------
### Create Vanilla Zookeeper Client Bean
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-zookeeper/README.adoc
This Java bean creates a vanilla Zookeeper client. It injects the host and port properties provided by the embedded-zookeeper module and establishes a connection. Ensure the Zookeeper client is properly closed after use.
```java
private static final int DEFAULT_SESSION_TIMEOUT_MS = 60000;
@Bean(destroyMethod = "close")
public ZooKeeper zookeeperClient(@Value("${embedded.zookeeper.host}") String host,
@Value("${embedded.zookeeper.port}") int port) throws Exception {
CountDownLatch connSignal = new CountDownLatch(1);
String connectionString = host + ":" + port;
ZooKeeper zooKeeper = new ZooKeeper(connectionString, DEFAULT_SESSION_TIMEOUT_MS, event -> {
if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {
connSignal.countDown();
}
});
connSignal.await();
return zooKeeper;
}
```
--------------------------------
### Configure Elasticsearch Client Endpoints
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-elasticsearch/README.adoc
Set the Elasticsearch client endpoints in your test `application.properties` to connect to the embedded instance.
```properties
spring.data.elasticsearch.client.reactive.endpoints=${embedded.elasticsearch.host}:${embedded.elasticsearch.httpPort}
spring.elasticsearch.rest.uris=http://${embedded.elasticsearch.host}:${embedded.elasticsearch.httpPort}
```
--------------------------------
### Attach Container Log Output
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
Set this property to true to attach and display the output log from the embedded container. The default value is false.
```yaml
embedded.redis.attachContainerLog=true
```
--------------------------------
### Configure Storage Service Bean with Embedded Google Storage
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-google-storage/README.adoc
This Java configuration defines a Storage service bean using properties injected from the embedded Google Storage container. Ensure the `embedded-google-storage` dependency is included.
```java
@Configuration
class StorageConfig {
@Bean
Storage storage(
@Value("${embedded.google.storage.endpoint}") String storageHost,
@Value("${embedded.google.storage.project-id}") String projectId) {
return StorageOptions.newBuilder()
.setHost(storageHost)
.setProjectId(projectId)
.setCredentials(NoCredentials.getInstance())
.build()
.getService();
}
}
```
--------------------------------
### Add Maven dependency for embedded-keydb
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-keydb/README.adoc
Include this dependency in your pom.xml to enable the embedded KeyDB container support.
```xml
com.playtika.testcontainers
embedded-keydb
test
```
--------------------------------
### Maven Dependency for Embedded Google Storage
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-google-storage/README.adoc
Add this dependency to your pom.xml to include the embedded-google-storage module in your project for testing purposes.
```xml
com.playtika.testcontainers
embedded-google-storage
test
```
--------------------------------
### Add Maven dependency for embedded-cassandra
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-cassandra/README.adoc
Include this dependency in your pom.xml to enable the embedded Cassandra module for testing.
```xml
com.playtika.testcontainers
embedded-cassandra
test
```
--------------------------------
### Create NATS Connection Bean
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-nats/README.adoc
Define a Spring Bean to establish a NATS connection using properties provided by the embedded NATS module. Ensure the connection is closed on destroy.
```java
@Bean(destroyMethod = "close")
public Connection natsConnection(@Value("${embedded.nats.host}") String host,
@Value("${embedded.nats.port}") int port) {
Options options = new Options.Builder()
.server(String.format("nats://%s:%s", host, port))
.build();
return Nats.connect(options);
}
```
--------------------------------
### Inject ToxiProxy Client for NATS
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-nats/README.adoc
Inject the ToxiProxyClientProxy bean into your tests to control the NATS container's network behavior.
```java
@Autowired
ToxiproxyClientProxy natsContainerProxy;
```
--------------------------------
### Maven Dependency for Embedded OpenSearch
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-opensearch/README.adoc
Add this dependency to your pom.xml to include the embedded OpenSearch module for testing.
```xml
com.playtika.testcontainers
embedded-opensearch
test
```
--------------------------------
### Enable Container Reuse
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/README.adoc
Set this property to true to enable the Testcontainers feature for reusing containers between test runs. Refer to the provided links for more details on container reuse.
```yaml
embedded.redis.reuseContainer=true
```
--------------------------------
### Configure Embedded Elasticsearch Properties
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-elasticsearch/README.adoc
Use these properties in `bootstrap.properties` or `application.properties` to customize the embedded Elasticsearch container.
```properties
embedded.elasticsearch.enabled=true
embedded.elasticsearch.reuseContainer=false
embedded.elasticsearch.dockerImage=docker.elastic.co/elasticsearch/elasticsearch:9.4.2
embedded.elasticsearch.indices=my_index
embedded.elasticsearch.waitTimeoutInSeconds=60
embedded.toxiproxy.proxies.elasticsearch.enabled=true
```
--------------------------------
### Spring Boot Application Properties for RabbitMQ Streams
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-rabbitmq/README.adoc
Configure application.properties to connect to the embedded RabbitMQ instance using its host, port, and credentials, specifically for RabbitMQ Streams.
```properties
spring.rabbitmq.stream.host=${embedded.rabbitmq.host}
spring.rabbitmq.stream.port=${embedded.rabbitmq.additionalPorts.5552}
spring.rabbitmq.stream.username=${embedded.rabbitmq.user}
spring.rabbitmq.stream.password=${embedded.rabbitmq.password}
spring.rabbitmq.stream.virtual-host=${embedded.rabbitmq.vhost}
```
--------------------------------
### Add Maven Dependency for Embedded MSSQL Server
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-mssqlserver/README.adoc
Include this dependency in your pom.xml to use the embedded MSSQL Server module. It is scoped for testing.
```xml
com.playtika.testcontainers
embedded-mssqlserver
test
```
--------------------------------
### Maven Dependency for Embedded PostgreSQL
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-postgresql/README.adoc
Add this dependency to your pom.xml to include the embedded PostgreSQL module for testing.
```xml
com.playtika.testcontainers
embedded-postgresql
test
```
--------------------------------
### Add LocalStack Maven Dependency
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-localstack/README.adoc
Include this dependency in your pom.xml to enable the embedded LocalStack module for testing.
```xml
com.playtika.testcontainers
embedded-localstack
test
```
--------------------------------
### Add Maven Dependency for Embedded DB2
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-db2/README.adoc
Include this dependency in your pom.xml to enable the embedded DB2 module.
```xml
com.playtika.testcontainers
embedded-db2
test
```
--------------------------------
### Configure Spring Data JDBC with Embedded ClickHouse
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-clickhouse/README.adoc
Use these properties in your test application.properties to configure Spring Data JDBC to connect to the embedded ClickHouse instance. Ensure the driver class name and URL are correctly set.
```properties
spring.datasource.driver-class-name=com.clickhouse.jdbc.Driver
spring.datasource.url=jdbc:clickhouse://${embedded.clickhouse.host}:${embedded.clickhouse.port}/${embedded.clickhouse.schema}
spring.datasource.username=${embedded.clickhouse.user}
spring.datasource.password=${embedded.clickhouse.password}
```
--------------------------------
### Add Maven Dependency for Embedded Elasticsearch
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-elasticsearch/README.adoc
Include this dependency in your pom.xml to enable embedded Elasticsearch testing.
```xml
com.playtika.testcontainers
embedded-elasticsearch
test
```
--------------------------------
### Add Maven Dependency for Embedded CockroachDB
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-cockroachdb/README.adoc
Include this dependency in your pom.xml to enable embedded CockroachDB support for testing. Ensure it's in the test scope.
```xml
com.playtika.testcontainers
embedded-cockroachdb
test
```
--------------------------------
### Application Configuration for SASL_PLAINTEXT
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-kafka/README.adoc
Configure your application's producer and consumer to use the SASL_PLAINTEXT security protocol with the embedded Kafka instance. This includes setting the bootstrap servers, security protocol, SASL mechanism, and JAAS configuration.
```properties
# Common for Producer and Consumer
bootstrap.servers=${embedded.kafka.saslPlaintext.brokerList}
security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required \
username="${embedded.kafka.saslPlaintext.user}" \
password="${embedded.kafka.saslPlaintext.password}";
# Consumer group.id could be any as ACLs are applied to wildcard group
```
--------------------------------
### Add Maven Dependency for Embedded MySQL
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-mysql/README.adoc
Include this dependency in your pom.xml to use the embedded-mysql module for testing.
```xml
com.playtika.testcontainers
embedded-mysql
test
```
--------------------------------
### Embedded Cassandra Configuration
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-cassandra/README.adoc
Configuration properties and produced beans for the embedded Cassandra container.
```APIDOC
## Embedded Cassandra Configuration
### Description
Configures the embedded Cassandra container behavior and retrieves connection details for integration tests.
### Configuration Parameters (bootstrap.properties)
- **embedded.cassandra.enabled** (boolean) - Optional - Default: true. Enables the embedded Cassandra container.
- **embedded.cassandra.reuseContainer** (boolean) - Optional - Default: false. Reuses the container across tests.
- **embedded.cassandra.keyspace-name** (string) - Optional - Default: 'embedded'. The keyspace name to use.
- **embedded.cassandra.replication-factor** (integer) - Optional - Default: 1. The replication factor for the keyspace.
- **embedded.cassandra.dockerImage** (string) - Optional - Default: 'cassandra:5.0'. The Docker image tag to use.
- **embedded.toxiproxy.proxies.cassandra.enabled** (boolean) - Optional - Enables ToxiProxy for the Cassandra container.
### Produced Properties
- **embedded.cassandra.host** (string) - The host address of the Cassandra container.
- **embedded.cassandra.port** (integer) - The port of the Cassandra container.
- **embedded.cassandra.datacenter** (string) - The datacenter name.
- **embedded.cassandra.keyspace-name** (string) - The configured keyspace name.
- **embedded.cassandra.toxiproxy.host** (string) - The ToxiProxy host address.
- **embedded.cassandra.toxiproxy.port** (integer) - The ToxiProxy port.
- **embedded.cassandra.networkAlias** (string) - The network alias for the container.
- **embedded.cassandra.internalPort** (integer) - The internal port of the container.
### Produced Beans
- **cassandraContainerProxy** (ToxiproxyClientProxy) - Proxy client for the Cassandra container.
```
--------------------------------
### Add Maven Dependency for Embedded Prometheus
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-prometheus/README.adoc
Include this dependency in your pom.xml to enable the embedded Prometheus integration for testing.
```xml
com.playtika.testcontainers
embedded-prometheus
test
```
--------------------------------
### Add Maven Dependency for Embedded MinIO
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-minio/README.adoc
Include this dependency in your pom.xml for test scope to enable the embedded MinIO module.
```xml
com.playtika.testcontainers
embedded-minio
test
```
--------------------------------
### Maven Dependency
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-vault/README.adoc
Add this dependency to your project's pom.xml to include the embedded Vault module.
```APIDOC
## Maven Dependency
Add the following to your pom.xml:
.pom.xml
[source,xml]
----
com.playtika.testcontainers
embedded-vault
test
----
```
--------------------------------
### Add Embedded MemSQL Maven Dependency
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-memsql/README.adoc
Include this dependency in your pom.xml for test scope to enable embedded MemSQL.
```xml
com.playtika.testcontainers
embedded-memsql
test
```
--------------------------------
### Add Maven Dependency for Embedded ClickHouse
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-clickhouse/README.adoc
Include this dependency in your pom.xml to enable embedded ClickHouse support for testing.
```xml
com.playtika.testcontainers
embedded-clickhouse
test
```
--------------------------------
### Add Maven Dependency for Embedded Neo4j
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-neo4j/README.adoc
Include this dependency in your pom.xml for test scope to enable embedded Neo4j support.
```xml
com.playtika.testcontainers
embedded-neo4j
test
```
--------------------------------
### Inject ToxiProxyClientProxy for VictoriaMetrics
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-victoriametrics/README.adoc
Inject this bean into your tests to manipulate the ToxiProxy instance controlling the VictoriaMetrics container.
```java
@Autowired
ToxiproxyClientProxy victoriaMetricsContainerProxy;
```
--------------------------------
### Add Maven Dependency for Embedded VictoriaMetrics
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-victoriametrics/README.adoc
Include this dependency in your pom.xml to use the embedded-victoriametrics module for testing.
```xml
com.playtika.testcontainers
embedded-victoriametrics
test
```
--------------------------------
### Add Maven Dependency for Embedded ToxiProxy
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-toxiproxy/README.adoc
Include this dependency in your pom.xml to enable the embedded ToxiProxy module for testing.
```xml
com.playtika.testcontainers
embedded-toxiproxy
test
```
--------------------------------
### Maven Dependency for Embedded MariaDB
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-mariadb/README.adoc
Add this dependency to your pom.xml to include the embedded MariaDB module for testing.
```xml
com.playtika.testcontainers
embedded-mariadb
test
```
--------------------------------
### Maven Dependency for Embedded RabbitMQ
Source: https://github.com/playtikaoss/testcontainers-spring-boot/blob/develop/embedded-rabbitmq/README.adoc
Add this dependency to your pom.xml to include the embedded-RabbitMQ module in your test scope.
```xml
com.playtika.testcontainers
embedded-rabbitmq
test
```