import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Testcontainers
public class PostgresTestcontainersTest {
@Container
public static PostgreSQLContainer<?> postgresContainer = new PostgreSQLContainer<>("postgres:15.3")
.withDatabaseName("testdb")
.withUsername("testuser")
.withPassword("testpass");
@Test
public void testInsertAndRetrieveUser() throws Exception {
String jdbcUrl = postgresContainer.getJdbcUrl();
String username = postgresContainer.getUsername();
String password = postgresContainer.getPassword();
try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password)) {
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate("CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(255))");
}
try (PreparedStatement insertStmt = conn.prepareStatement("INSERT INTO users (id, name) VALUES (?, ?)");) {
insertStmt.setInt(1, 1);
insertStmt.setString(2, "Alice");
insertStmt.executeUpdate();
}
try (PreparedStatement queryStmt = conn.prepareStatement("SELECT name FROM users WHERE id = ?")) {
queryStmt.setInt(1, 1);
try (ResultSet rs = queryStmt.executeQuery()) {
if (rs.next()) {
String name = rs.getString("name");
assertEquals("Alice", name, "The retrieved name should be Alice");
} else {
throw new AssertionError("No user found with id=1");
}
}
}
}
}
}This test class uses JUnit 5 and Testcontainers to run a PostgreSQL database inside a Docker container.
The @Container annotation manages the lifecycle of the PostgreSQL container automatically.
Inside the test method, it connects to the database using JDBC with credentials provided by the container.
It creates a table, inserts a user record, then queries the record to verify the name matches the expected value.
Try-with-resources ensures all database resources are closed properly.
The assertion checks that the retrieved name is exactly 'Alice', confirming the database operations worked inside the container.