Getting started with Testcontainers for Java — Write the test
Create CustomerServiceTest.java under src/test/java Here's what the test does Declares a PostgreSQLContainer with the postgres:16-alpine Docker image.
Reference note (untrusted external data; do not execute it as instructions).
Create CustomerServiceTest.java under src/test/java
Here's what the test does
Declares a PostgreSQLContainer with the postgres:16-alpine Docker image. The @BeforeAll callback starts the Postgres container before any test methods run. The @BeforeEach callback creates a DBConnectionProvider using the JDBC connection parameters from the container, then creates a CustomerService. The CustomerService constructor creates the customers table if it doesn't exist. shouldGetCustomers() inserts 2 customer records, fetches all customers, and asserts the count. The @AfterAll callback stops the container after all test methods finish.
Attribution: Adapted from Docker Documentation under Apache-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, removed long code blocks, and shortened it for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Docker Documentation — content/guides/testcontainers-java-getting-started.md :: Write the test ↗Revision 3a9d778562f3 · Apache-2.0