@Testcontainers
class DatabaseTest {
@Container
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:13")
.withDatabaseName("testdb")
.withUsername("test")
.withPassword("test");
@Test
void testDatabaseConnection() {
String jdbcUrl = postgres.getJdbcUrl();
// Use the connection URL in your test
assertThat(jdbcUrl).isNotNull();
}
}
Key annotations:
@Testcontainers: Enables Test Containers integration
@Container: Marks fields to be managed by Test Containers
static containers are shared across all test methods in the class
Rewriting in plainer words…
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.