0
0
Spring Bootframework~10 mins

Test containers for database testing in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Test containers for database testing
Start Test
Initialize Test Container
Start Database Container
Run Application Tests
Connect to Container DB
Execute DB Operations
Stop Container
End Test
This flow shows how a test container starts a database, runs tests using it, then stops the container after tests finish.
Execution Sample
Spring Boot
import org.testcontainers.containers.PostgreSQLContainer;

static final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15");

@PostConstruct
void startContainer() {
  postgres.start();
}

@Test
void testDbConnection() {
  // test logic here
  // e.g., use postgres.getJdbcUrl(), postgres.getUsername(), postgres.getPassword()
}
This code starts a PostgreSQL test container before tests and runs a test connecting to it.
Execution Table
StepActionContainer StateDB ConnectionTest Status
1Initialize PostgreSQLContainer objectNot startedNo connectionNot started
2Call postgres.start()Container startingNo connectionNot started
3Container fully startedRunningConnection readyNot started
4Run test methodRunningConnectedRunning
5Execute DB queriesRunningConnectedRunning
6Test completes successfullyRunningConnectedPassed
7Stop containerStoppedNo connectionFinished
💡 Test container stops after tests complete, releasing resources.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 6Final
postgresCreatedStartingRunningRunningStopped
dbConnectionnullnullopenopenclosed
testStatusNot startedNot startedNot startedPassedFinished
Key Moments - 3 Insights
Why do we need to start the container before running tests?
The container must be running to provide a real database for tests. See execution_table step 3 where the container is running and connection is ready.
What happens if the container is not stopped after tests?
Resources stay used and may cause conflicts or memory leaks. Step 7 shows stopping the container to clean up.
How does the test connect to the database inside the container?
The test uses connection info from the running container (step 4-5), ensuring tests run against a real isolated DB.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the database connection become ready?
AStep 2
BStep 4
CStep 3
DStep 7
💡 Hint
Check the 'DB Connection' column in execution_table rows.
According to variable_tracker, what is the state of 'postgres' after step 6?
ACreated
BRunning
CStarting
DStopped
💡 Hint
Look at the 'postgres' row and the 'After Step 6' column.
If the container is not stopped, what would be the final value of 'testStatus' in variable_tracker?
APassed
BRunning
CNot started
DFinished
💡 Hint
Stopping container changes 'testStatus' to 'Finished' as per variable_tracker final column.
Concept Snapshot
Test containers start a real database in a container before tests run.
Tests connect to this isolated DB for realistic testing.
After tests, the container stops to free resources.
Use @PostConstruct or setup methods to start containers.
Use container-provided connection info in tests.
Always stop containers to avoid resource leaks.
Full Transcript
Test containers for database testing work by starting a real database inside a container before tests run. The test code connects to this database using connection details provided by the container. Tests execute queries against this isolated database, ensuring realistic and clean test environments. After tests finish, the container is stopped to release resources and avoid conflicts. This process involves initializing the container object, starting it, running tests connected to it, and stopping it after tests complete. Variables like the container state, database connection, and test status change step-by-step during this flow.