TestContainers class final

The containers of a test class: what the generated code of a class with @Container() fields calls.

start starts the containers one after the other, in the order of the fields, before the class's context (and stops those already started when one fails); stop removes them in reverse order after the context closed. registerConnections adds the properties of the @ServiceConnection() containers to the context's dynamic properties, which win over every other source. skipReason decides whether the class runs at all.

A test written as functions uses it the same way, or starts its GenericContainer in setUpAll by hand:

void main() {
  final postgres = PostgresContainer();
  final containers = TestContainers({'postgres': postgres},
      serviceConnections: {'postgres'});
  setUpAll(containers.start);
  tearDownAll(containers.stop);
  final app = springBootTest(springApplication,
      dynamicProperties: containers.registerConnections);
  // ...
}

Spring: TestcontainersExtension of @Testcontainers, and the ContainerConnectionDetailsFactorys behind @ServiceConnection.

Constructors

TestContainers(Map<String, Object?> containers, {Set<String> serviceConnections = const {}})
The containers by name (Class.field, for the error messages), and the names of those that are service connections.
const

Properties

containers → Map<String, Object?>
The containers to start, by name, in order.
final
hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
serviceConnections → Set<String>
The names of the containers whose connection properties are put into the context.
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
registerConnections(DynamicPropertyRegistry registry) → void
Adds the connection properties of the service connections to registry: each is read when the application asks for it, so it sees the mapped ports of the running container.
start() → Future<void>
Starts every container, in order. When one fails, the ones started are stopped and the error is rethrown.
stop() → Future<void>
Removes every container, in reverse order; all are tried, and the first error is rethrown at the end.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Properties

required → bool
Whether CONTAINERS_REQUIRED=true is set: a missing Docker fails the tests instead of skipping them. Set it in CI.
no setter

Static Methods

skipReason({bool skipWithoutDocker = true}) → String?
Why tests that need containers cannot run here, or null when they can: no Docker socket found.