PostgresContainer class
PostgreSQL in a container, ready for a test: a database, a user and a
password set through the image's environment, and a wait that outlasts
the image's two-step start: a temporary server initialises the database
and stops, then the real one starts, so the container is ready when
database system is ready to accept connections was logged twice (the
wait of Testcontainers' PostgreSQLContainer), the port accepts
connections, and the server answers the protocol's first message
through the published port.
url and jdbcUrl are in the form spring_boot_starter_data reads
(postgresql://user:password@host:port/database); as a
@ServiceConnection() it sets spring.datasource.url,
spring.datasource.username and spring.datasource.password for the
test's context, so a repository test runs on this server with no other
configuration. This package does not depend on the data starter: the
properties are plain strings.
@DataTest(replaceDatabase: false) // the real Postgres, not SQLite
class BookRepositoryTest {
@Container()
@ServiceConnection()
static final postgres = PostgresContainer('postgres:17-alpine');
// ...
}
Testcontainers (Java): PostgreSQLContainer (getJdbcUrl(),
getUsername(), getPassword()), with Spring Boot's @ServiceConnection.
- Inheritance
-
- Object
- GenericContainer
- PostgresContainer
- Implemented types
Constructors
- PostgresContainer([String image = 'postgres:17-alpine'])
-
PostgreSQL from
image(postgres:17-alpineunless given), with the database, user and passwordtest.
Properties
-
connectionProperties
→ Map<
String, String> -
The properties that point an application at the started container,
such as
spring.datasource.url. Read after the container started: the values hold its host and mapped ports.no setteroverride - containerId → String
-
The container's id; throws before start.
no setterinherited
- databaseName → String
-
The database that is created and the tests connect to.
no setter
- docker → DockerClient
-
The client of the daemon the container runs on.
no setterinherited
-
exposedPorts
→ List<
int> -
The container ports published with
withExposedPorts.no setterinherited - hashCode → int
-
The hash code for this object.
no setterinherited
- host → String
-
The host the published ports are reached on:
127.0.0.1for a local daemon.no setterinherited - image → String
-
The image the container is created from.
finalinherited
- isStarted → bool
-
Whether start succeeded and stop was not called since.
no setterinherited
- jdbcUrl → String
-
The address of the database without credentials,
postgresql://host:port/database: with username and password asspring.datasource.*properties.no setter - password → String
-
The user's password.
no setter
- remaining → Duration
-
How long the strategy may still take; it is the container's startup
timeout, less the time already spent.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- url → String
-
The address with the credentials,
postgresql://user:password@host:port/database, as the data starter's test settings (SPRING_TEST_POSTGRES_URL) have it.no setter - username → String
-
The user that is created.
no setter
- waitStrategy → WaitStrategy
-
The wait strategy, for modules that extend it.
no setterinherited
Methods
-
baseUrl(
int port, {String scheme = 'http'}) → String -
scheme://host:portof the container'sport:httpunlessschemesays otherwise.inherited -
execInContainer(
List< String> command) → Future<DockerExecResult> -
Runs
commandin the started container and waits for it.inherited -
getLogs(
) → Future< String> -
Everything the container printed so far, standard output and error
together.
inherited
-
inspect(
) → Future< DockerContainerState> -
The container's state, as the daemon reports it now.
inherited
-
logs(
) → Stream< DockerLogLine> -
The container's output from its start, following it until it stops.
inherited
-
mappedPort(
int port) → int -
The port on host that the container's
portis published on; the port must be listed in withExposedPorts, and the container started.inherited -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
poll(
String description, Future< bool> check(), {Duration interval = const Duration(milliseconds: 100)}) → Future<void> -
Calls
checkeveryintervaluntil it returnstrue.inherited -
start(
) → Future< void> -
Pulls the image if it is missing, creates the container, copies the
files in, starts it and waits until it is ready.
inherited
-
stop(
) → Future< void> -
Removes the container (killing it when it still runs). A container
that is not started, or was found again through withReuse, is left
as it is.
inherited
-
toString(
) → String -
A string representation of this object.
override
-
waitingFor(
WaitStrategy strategy) → GenericContainer -
Decides when the started container counts as ready: see Wait. By
default
Wait.forListeningPort().inherited -
withCommand(
List< String> command) → GenericContainer -
Replaces the image's default command (
CMD) withcommand.inherited -
withCopyContent(
Object content, String containerPath, {int mode = 420}) → GenericContainer -
Creates the file
containerPathwithcontent(aStringor bytes) in the container before it starts.inherited -
withCopyFile(
String hostPath, String containerPath, {int mode = 420}) → GenericContainer -
Copies the file at
hostPathtocontainerPathin the container before it starts. A relativehostPathstarts at the working directory (the package, underdart test); the file is read at start.modeis the permission (0644by default, as0x1A4).inherited -
withDatabaseName(
String name) → PostgresContainer -
Names the database (
POSTGRES_DB). -
withEntrypoint(
List< String> entrypoint) → GenericContainer -
Replaces the image's entry point with
entrypoint.inherited -
withEnv(
String name, String value) → GenericContainer -
Sets the environment variable
name.inherited -
withEnvironment(
Map< String, String> variables) → GenericContainer -
Sets several environment variables.
inherited
-
withExposedPorts(
List< int> ports) → GenericContainer -
Publishes the container's TCP
portson random free ports of the host; mappedPort says which.inherited -
withHealthCheck(
List< String> test, {Duration interval = const Duration(seconds: 1), Duration timeout = const Duration(seconds: 3), int retries = 10}) → GenericContainer -
Gives the container a health check, for
Wait.forHealthcheck()and for images that define none:testis the command in exec form (['CMD-SHELL', 'pg_isready -U test']), run everyinterval; afterretriesfailures the container isunhealthy.inherited -
withInitScript(
String hostPath) → PostgresContainer -
Runs the SQL file at
hostPathwhen the database is created (the image's/docker-entrypoint-initdb.d/); files run in the order of their names. -
withLabel(
String name, String value) → GenericContainer -
Adds the label
name=valueto the container.inherited -
withPassword(
String value) → PostgresContainer -
Sets the user's password (
POSTGRES_PASSWORD). -
withReuse(
bool reuse) → GenericContainer -
Keeps the container alive after the tests, for the next run to use
again (off by default, and ignored when the
CIenvironment variable is set).inherited -
withStartupTimeout(
Duration timeout) → GenericContainer -
How long the wait strategy may take (60 seconds by default); the image
pull is not counted.
inherited
-
withUsername(
String name) → PostgresContainer -
Names the user (
POSTGRES_USER).
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited