Spring-TestContainers is a Java library that makes it easier to write integration tests with Testcontainers, especially when you're using Spring or Spring Boot. It handles the setup and lifecycle of containers for you, so you can focus on testingβnot boilerplate.
We originally built this for FlowInquiry to make our own testing smoother. It worked so well, we decided to share it as a standalone library so other teams can take advantage of it too.
Setting up Testcontainers in Spring-based projects often involves boilerplate code and manual configuration. Spring-TestContainers eliminates that overhead with a clean, annotation-driven approach that:
-
Reduces setup to a single annotation (@EnablePostgreSQL, @EnableMySQL, etc.)
-
Automatically manages container lifecycle
-
Auto-configures Spring environment with database connection details
Spring-TestContainers provides out-of-the-box support for the following containers. You can enable each one via a dedicated annotation in your test classes:
Spring-TestContainers provides out-of-the-box support for the following containers. You can enable each one via a dedicated annotation in your test classes:
Container | Annotation | Example Usage | Notes |
---|---|---|---|
PostgreSQL | @EnablePostgresContainer |
@EnablePostgresContainer(version = "15") |
Uses PostgreSQLContainer |
MySQL | @EnableMySQLContainer |
@EnableMySQLContainer(version = "8") |
Uses MySQLContainer |
Kafka | @EnableKafkaContainer |
@EnableKafkaContainer(version = "3.9.1") |
Use KafkaContainer |
Ollama (AI) | @EnableOllamaContainer |
@EnableOllamaContainer(model = "llama2") |
Starts Ollama with auto-pull |
The following table demonstrates the difference between using TestContainers with Spring directly and using Spring-TestContainers:
Feature | Plain TestContainers with Spring | Spring-TestContainers |
---|---|---|
Setup | Manual container and Spring config | Single annotation |
Boilerplate | ~50 lines | ~5 lines |
Container Lifecycle | Manual or via JUnit extension | Automatic |
Spring Environment | Manually wired | Auto-configured |
Maintenance | More code to maintain | Minimal code |
Focus | Infrastructure + test logic | Pure test logic |
Learning Curve | High | Low |
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import javax.sql.DataSource;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = PostgresSpringTest.TestConfig.class)
@Testcontainers
class PostgresSpringTest {
@Container
private static final PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>("postgres:16.3")
.withDatabaseName("test")
.withUsername("test")
.withPassword("test");
@Autowired
private DataSource dataSource;
@Test
void testDatabaseConnection() {
// Your test code here using the dataSource
// The container is automatically started by the @Testcontainers annotation
// and the dataSource is configured to connect to it
}
@Configuration
static class TestConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl(postgres.getJdbcUrl());
dataSource.setUsername(postgres.getUsername());
dataSource.setPassword(postgres.getPassword());
return dataSource;
}
}
}
import io.flowinquiry.testcontainers.jdbc.postgresql.EnablePostgreSQL;
import org.junit.jupiter.api.Test;
@ExtendWith(SpringExtension.class)
@EnablePostgreSQL
class PostgresTest {
@Autowired
private DataSource dataSource;
@Test
void testDatabaseConnection() {
// Your test code here
// Container is automatically started and stopped
}
}
-
π§© Simple annotation API: @EnablePostgreSQL, @EnableMySQL, @EnableOllmaContainer
-
π Automatic container lifecycle management
-
π§ͺ JUnit 5 integration
-
π± Full Spring and Spring Boot support
-
π§° Extensible architecture for other databases
-
π Out-of-the-box support for PostgreSQL and MySQL
- Java 17 or higher
- JUnit 5
- Docker (for running the containers)
- Spring Framework 6.x (for Spring integration)
- Spring Boot 3.x (for Spring Boot integration)
Add the core library along with the database module(s) you plan to use. Each database has its own module, which includes everything needed to support that specific container and configuration.
// Add one or more of the following database modules
testImplementation("io.flowinquiry.testcontainers:postgresql:<!-- Replace with the latest version -->") // PostgreSQL support
testImplementation("io.flowinquiry.testcontainers:mysql:<!-- Replace with the latest version -->") // MySQL support
testImplementation("io.flowinquiry.testcontainers:kafka:<!-- Replace with the latest version -->")
testImplementation("io.flowinquiry.testcontainers:ollama:<!-- Replace with the latest version -->") // Ollama support
<!-- Add one or more of the following database modules -->
<!-- Add this dependency to test Postgres database -->
<dependency>
<groupId>io.flowinquiry.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version><!-- Replace with the latest version --></version>
<scope>test</scope>
</dependency>
<dependency>
<!-- Add this dependency to test MySQL database -->
<dependency>
<groupId>io.flowinquiry.testcontainers</groupId>
<artifactId>mysql</artifactId>
<version><!-- Replace with the latest version --></version>
<scope>test</scope>
</dependency>
<!-- Add this dependency to test Kafka -->
<dependency>
<groupId>io.flowinquiry.testcontainers</groupId>
<artifactId>kafka</artifactId>
<version><!-- Replace with the latest version --></version>
<scope>test</scope>
</dependency>
<!-- Add this dependency to test Ollama container -->
<dependency>
<groupId>io.flowinquiry.testcontainers</groupId>
<artifactId>ollama</artifactId>
<version><!-- Replace with the latest version --></version>
<scope>test</scope>
</dependency>
π As more databases are supported, simply add the corresponding module and TestContainers dependency.
// Spring Framework (without Spring Boot)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = YourSpringConfig.class)
@EnablePostgreSQL
class SpringPostgresTest {
@Autowired
private YourRepository repository;
@Test
void testWithSpring() {
// Spring environment is auto-configured with container details
}
}
// Spring Boot
@SpringBootTest
@EnablePostgreSQL
class SpringBootPostgresTest {
@Autowired
private YourRepository repository;
@Test
void testWithSpringBoot() {
// Spring Boot is auto-configured with container details
}
}
Currently, the following containers are supported:
- PostgreSQL
- MySQL
- Kafka
- Ollama
See the documentation for detailed instructions on how to use Spring TestContainers annotations and how to extend the library with your own custom modules.
The project includes several example modules demonstrating how to use Spring-TestContainers:
-
Spring Boot applications using JPA with PostgreSQL and MySQL
-
Show how to integrate containerized databases with minimal configuration
-
Spring Framework (no Boot) setup with JPA and PostgreSQL
-
Manual configuration for container-based testing
These examples provide a good starting point for integrating Spring-TestContainers into your own projects.
-
Spring Boot applications having Kafka producer and consumer
-
Show how to integrate kafka container with minimal configuration
-
Spring Boot applications using Spring AI and Ollama
-
Show how to test AI prompts with Ollama container
Contributions are welcome! If you'd like to add support for additional databases or improve the library, please:
- Fork the repository
- Create a feature branch
- Add your changes
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.