Skip to content

Infobip TestContainers Spring Boot Starter provides Spring Boot starters that ease the use of Testcontainers in test and local development scenarios.

License

Notifications You must be signed in to change notification settings

FraneJelavic/infobip-testcontainers-spring-boot-starter

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Infobip Testcontainers Spring Boot Starter

Maven Central Coverage Status

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)

Contents

Usage

MSSQL

Disclaimer: by using this testcontainer you accept the EULA for mssql docker image as required here.

Tests:

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
    username: SA
    password: A_Str0ng_Required_Password

Microsoft driver:

spring:
  datasource:
    url: jdbc:sqlserver://<host>:<port>;database=FooBarDb
    username: SA
    password: A_Str0ng_Required_Password

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. Docker container is mapped on random port so <port> placeholder is used and will be automatically substituted.

Local Development:

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:

profile.png

and update your local configuration (e.g. application-development.yaml):

jTDS:

spring:
  datasource:
    url: jdbc:jtds:sqlserver://<host>:<port>/FooBarDb_test_${user.name}
    username: SA
    password: A_Str0ng_Required_Password

Microsoft driver:

spring:
  datasource:
    url: jdbc:sqlserver://<host>:<port>;database=FooBarDb_test_${user.name}
    username: SA
    password: A_Str0ng_Required_Password

Docker image:

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

PostgreSQL

Tests:

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. Docker container is mapped on random port so <port> placeholder is used and will be automatically substituted.

Local Development:

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:

profile.png

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

Docker image:

To change the docker image used simply add the following property (e.g. in yaml):

testcontainers.postgresql.docker.image: postgres:10

Redis

Tests:

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. Docker container is mapped on random port so <port> placeholder is used and will be automatically substituted.

Local Development:

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:

profile.png

and update your local configuration (e.g. application-development.yaml):

spring:
  redis:
    url: redis://<host>:<port>

Docker image:

To change the docker image used simply add the following property (e.g. in yaml):

testcontainers.redis.docker.image: redis:5.0.7-alpine

Kafka

Automatic topic creation

Format: <topicName>:<numPartitions>:<replicationFactor>

testcontainers.kafka.topics: test-topic:1:1, test-topic-2:1:1

Tests:

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. Docker container is mapped on random port so <port> placeholder is used and will be automatically substituted.

Local Development:

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:

profile.png

and update your local configuration (e.g. application-development.yaml):

spring:
  kafka:
    bootstrap-servers: <host>:<port>

Docker image:

To change the docker image version used simply add the following property (e.g. in yaml):

testcontainers.kafka.docker.image.version: 2.1.0

RabbitMq

Tests:

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>

Test Configuration:

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 name test.queue
  • testExchange creates exchange with name test.exchange
  • bindToTestExchange tells Rabbit to send any message sent to test exchange, with key of value test.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.

Local Development:

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:

profile.png

Docker image:

To change the docker image used simply add the following property (e.g. in yaml):

testcontainers.rabbit.docker.image: rabbitmq:3.6.14-alpine

Contributing

If you have an idea for a new feature or want to report a bug please use the issue tracker.

Pull requests are welcome!

License

This library is licensed under the Apache License, Version 2.0.

About

Infobip TestContainers Spring Boot Starter provides Spring Boot starters that ease the use of Testcontainers in test and local development scenarios.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%