Sharing a powerful tool you should know about
Why Use Testcontainers?
- Say goodbye to mocking external dependencies
- No need to manually install or clean up services for testing
- Run parallel tests without interference or port conflicts
- No more hunting for in-memory alternatives
- And much more — check out the Getting Started Guide
Example
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.PostgreSQLContainer;
public class PostgresTest {
@Test
void testDatabase() {
try (PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15.1")) {
postgres.start();
String jdbcUrl = postgres.getJdbcUrl();
String username = postgres.getUsername();
String password = postgres.getPassword();
// Use these credentials to run your integration test
}
}
}
⚡ Bonus: Reuse Containers for Faster Tests
In long test suites, container spin-up time can become an issue. Testcontainers offers reusable containers to persist containers across test runs—perfect for speeding up local development.
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15.1")
.withReuse(true);