Testcontainers Postgres Extension with advanced testing capabilities.
Features:
- Container easy run per method, per class, per execution.
- Container easy migration with Flyway / Liquibase.
- Container easy connection injection with asserts.
Gradle
testImplementation "io.goodforgod:testcontainers-extensions-postgres:0.15.0"Maven
<dependency>
<groupId>io.goodforgod</groupId>
<artifactId>testcontainers-extensions-postgres</artifactId>
<version>0.15.0</version>
<scope>test</scope>
</dependency>Postgres JDBC Driver must be on classpath, if it is somehow not on your classpath already, don't forget to add:
Gradle
testRuntimeOnly "org.postgresql:postgresql:42.6.0"Maven
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.6.0</version>
<scope>test</scope>
</dependency>Test with container start in PER_RUN mode and migration per method will look like:
@TestcontainersPostgreSQL(mode = ContainerMode.PER_RUN,
migration = @Migration(
engine = Migration.Engines.FLYWAY,
apply = Migration.Mode.PER_METHOD,
drop = Migration.Mode.PER_METHOD))
class ExampleTests {
@ConnectionPostgreSQL
private JdbcConnection connection;
@Test
void test() {
connection.execute("INSERT INTO users VALUES(1);");
var usersFound = connection.queryMany("SELECT * FROM users;", r -> r.getInt(1));
assertEquals(1, usersFound.size());
}
}JdbcConnection is an abstraction with asserting data in database container and easily manipulate container connection settings.
You can inject connection via @ConnectionPostgreSQL as field or method argument or manually create it from container or manual settings.
class ExampleTests {
private static final PostgreSQLContainer<?> container = new PostgreSQLContainer<>();
@Test
void test() {
container.start();
JdbcConnection connection = JdbcConnection.forContainer(container);
connection.execute("INSERT INTO users VALUES(1);");
}
}Migrations allow easily migrate database between test executions and drop after tests.
You can migrate container via @TestcontainersPostgreSQL#migration annotation parameter or manually using JdbcConnection.
@TestcontainersPostgreSQL
class ExampleTests {
@Test
void test(@ConnectionPostgreSQL JdbcConnection connection) {
connection.migrationEngine(Migration.Engines.FLYWAY).apply("db/migration");
connection.execute("INSERT INTO users VALUES(1);");
connection.migrationEngine(Migration.Engines.FLYWAY).drop("db/migration");
}
}Available migration engines:
@TestcontainersPostgreSQL - allow automatically start container with specified image in different modes without the need to configure it.
Available containers modes:
PER_RUN- start container one time per test execution. (Containers must have same instance, e.g. compare by==)PER_CLASS- start new container each test class.PER_METHOD- start new container each test method.
Simple example on how to start container per class, no need to configure container:
@TestcontainersPostgreSQL(mode = ContainerMode.PER_CLASS)
class ExampleTests {
@Test
void test(@ConnectionPostgreSQL JdbcConnection connection) {
assertNotNull(connection);
}
}That's all you need.
It is possible to customize image with annotation image parameter.
Image also can be provided from environment variable:
@TestcontainersPostgreSQL(image = "${MY_IMAGE_ENV|postgres:17.6-alpine}")
class ExampleTests {
@Test
void test() {
// test
}
}Image syntax:
- Image can have static value:
postgres:17.6-alpine - Image can be provided via environment variable using syntax:
${MY_IMAGE_ENV} - Image environment variable can have default value if empty using syntax:
${MY_IMAGE_ENV|postgres:17.6-alpine}
When you need to manually configure container with specific options, you can provide such container as instance that will be used by @TestcontainersPostgreSQL,
this can be done using @ContainerPostgreSQL annotation for container.
@TestcontainersPostgreSQL(mode = ContainerMode.PER_CLASS)
class ExampleTests {
@ContainerPostgreSQL
private static final PostgreSQLContainer<?> container = new PostgreSQLContainer<>()
.withDatabaseName("user")
.withUsername("user")
.withPassword("user");
@Test
void test(@ConnectionPostgreSQL JdbcConnection connection) {
assertEquals("user", connection.params().database());
assertEquals("user", connection.params().username());
assertEquals("user", connection.params().password());
}
}In case you want to enable Network.SHARED for containers you can do this using network & shared parameter in annotation:
@TestcontainersPostgreSQL(network = @Network(shared = true))
class ExampleTests {
@Test
void test() {
// test
}
}Default alias will be created by default, even if nothing was specified (depends on implementation).
You can provide also custom alias for container. Alias can be extracted from environment variable also or default value can be provided if environment is missing.
In case specified environment variable is missing default alias will be created:
@TestcontainersPostgreSQL(network = @Network(alias = "${MY_ALIAS_ENV|my_default_alias}"))
class ExampleTests {
@Test
void test() {
// test
}
}Image syntax:
- Image can have static value:
my-alias - Image can be provided via environment variable using syntax:
${MY_ALIAS_ENV} - Image environment variable can have default value if empty using syntax:
${MY_ALIAS_ENV|my-alias-default}
JdbcConnection - can be injected to field or method parameter and used to communicate with running container via @ConnectionPostgreSQL annotation.
JdbcConnection provides connection parameters, useful asserts, checks, etc. for easier testing.
@TestcontainersPostgreSQL(mode = ContainerMode.PER_CLASS, image = "postgres:17.6-alpine")
class ExampleTests {
@ConnectionPostgreSQL
private JdbcConnection connection;
@Test
void test() {
connection.execute("CREATE TABLE users (id INT NOT NULL PRIMARY KEY);");
connection.execute("INSERT INTO users VALUES(1);");
connection.assertInserted("INSERT INTO users VALUES(2);");
var usersFound = connection.queryMany("SELECT * FROM users;", r -> r.getInt(1));
assertEquals(2, usersFound.size());
connection.assertQueriesEquals(2, "SELECT * FROM users;");
}
}Isolation controls logical connection isolation inside a started container.
Default value is @Isolation(Isolation.Mode.DISABLED). Disabled isolation preserves regular behavior: injected connection points to the container database and migrations run directly against that database according to Migration.Mode.
Isolation.Mode.PER_METHOD reuses the same physical container but creates a separate generated database for every test method. Field and method argument injection receive a JdbcConnection configured for that generated database.
Isolation.Mode.PER_METHOD has lifecycle restrictions:
- Field injection requires JUnit default
TestInstance.Lifecycle.PER_METHOD. TestInstance.Lifecycle.PER_CLASSis rejected.- Constructor injection is rejected.
@BeforeAllparameter injection is rejected.
@TestcontainersPostgreSQL(mode = ContainerMode.PER_RUN,
isolation = @Isolation(Isolation.Mode.PER_METHOD),
migration = @Migration(
engine = Migration.Engines.FLYWAY,
apply = Migration.Mode.PER_METHOD,
drop = Migration.Mode.NONE))
class ExampleTests {
@ConnectionPostgreSQL
private JdbcConnection connection;
@Test
void test(@ConnectionPostgreSQL JdbcConnection parameter) {
assertSame(connection, parameter);
connection.execute("INSERT INTO users VALUES(1);");
}
}In case you want to use some external Postgres instance that is running in CI or other place for tests (due to docker limitations or other), you can use special environment variables and extension will use them to propagate connection and no Postgres containers will be running in such case.
Special environment variables:
EXTERNAL_TEST_POSTGRES_JDBC_URL- Postgres instance JDBC url.EXTERNAL_TEST_POSTGRES_USERNAME- Postgres instance username (optional).EXTERNAL_TEST_POSTGRES_PASSWORD- Postgres instance password (optional).EXTERNAL_TEST_POSTGRES_HOST- Postgres instance host (optional if JDBC url specified).EXTERNAL_TEST_POSTGRES_PORT- Postgres instance port (optional if JDBC url specified).EXTERNAL_TEST_POSTGRES_DATABASE- Postgres instance database (postgresby default) (optional if JDBC url specified)
Use can use either EXTERNAL_TEST_POSTGRES_JDBC_URL to specify connection with username & password combination
or use combination of EXTERNAL_TEST_POSTGRES_HOST & EXTERNAL_TEST_POSTGRES_PORT & EXTERNAL_TEST_POSTGRES_DATABASE.
EXTERNAL_TEST_POSTGRES_JDBC_URL env have higher priority over host & port & database.
@Migrations allow easily migrate database between test executions and drop after tests.
Annotation parameters:
engine- to use for migration.apply- parameter configures migration mode.drop- configures when to reset/drop/clear database.locations- configures locations where migrations are placed.strategy- configures how migrations are applied to isolated databases.
Available migration engines:
Given engine is Flyway and migration file named V1__flyway.sql is in resource directory on default path db/migration:
CREATE TABLE IF NOT EXISTS users
(
id INT NOT NULL PRIMARY KEY
);Test with container and migration per method will look like:
@TestcontainersPostgreSQL(mode = ContainerMode.PER_CLASS,
migration = @Migration(
engine = Migration.Engines.FLYWAY,
apply = Migration.Mode.PER_METHOD,
drop = Migration.Mode.PER_METHOD))
class ExampleTests {
@Test
void test(@ConnectionPostgreSQL JdbcConnection connection) {
connection.execute("INSERT INTO users VALUES(1);");
var usersFound = connection.queryMany("SELECT * FROM users;", r -> r.getInt(1));
assertEquals(1, usersFound.size());
}
}Migration.Strategy.DEFAULT runs Flyway or Liquibase directly against the active connection. This is the default and keeps existing migration behavior.
Migration.Strategy.TEMPLATE_CLONE is supported by Postgres for Isolation.Mode.PER_METHOD. The extension creates a migration_template_<hash> database, applies migrations to it once, closes the template connection, and then creates every test method database using CREATE DATABASE <generated> TEMPLATE <template>.
This is useful when migrations are expensive and tests need isolated databases:
@TestcontainersPostgreSQL(mode = ContainerMode.PER_RUN,
isolation = @Isolation(Isolation.Mode.PER_METHOD),
migration = @Migration(
engine = Migration.Engines.FLYWAY,
apply = Migration.Mode.PER_CLASS,
drop = Migration.Mode.NONE,
strategy = Migration.Strategy.TEMPLATE_CLONE))
class ExampleTests {
@Test
void test(@ConnectionPostgreSQL JdbcConnection connection) {
connection.execute("INSERT INTO users VALUES(1);");
connection.assertCountsEquals(1, "users");
}
}TEMPLATE_CLONE requires Isolation.Mode.PER_METHOD. Using it with default disabled isolation fails fast because it would change historical migration behavior.
This project licensed under the Apache License 2.0 - see the LICENSE file for details.