The project demonstrates how to use Spring Boot to create a simple Web app that
- integrates a PostgresQL database running in a Docker container
- uses Liquibase as a DB migration tool to : initialize the DB schema and populate the tables
- uses Testcontainers to create an integration test that loads the table and adds another record using JPA
The project was generated with Spring Boot 3.4.4 (and later upgraded to 4.1.1), Maven support and Java version 25. It also has a number of dependencies from the get-go:
- Spring Web (for RESTful controller that serves JSON)
- Docker Compose Support (will be started by
mvn spring:boot) - Spring Data JPA
- PostgreSQL Driver
- Liquibase Migration (using Liquibase as the DB source of truth)
- Spring Boot Actuator (mostly for application heath endpoints)
This project contains a Docker Compose file named compose.yaml.
In this file, the following services have been defined:
- postgres:
postgres:17.2
Liquibase is enabled via
spring:
liquibase:
change-log: classpath:db/changelog/db.changelog-master.xml
enabled: truesrc/main/resources/
├── db/
│ └── changelog/
│ ├── db.changelog-master.xml
│ ├── changes/
│ │ ├── create-customers-table.xml
│ │ └── insert-customers-data.xml
│ └── data/
│ └── customers.csv
Points to all changelog files
Changelog for creating customers table (and customers_seq sequence)
Changelog for pre-populating customers table and updating the customers_seqsequence
To enable real integration with dockerized DB backends, one needs to add the following Maven dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>The test class needs annotations such as @SpringBootTest and @Testcontainers and a @Container static variable need
to be declared:
@SpringBootTest
@Testcontainers
class IntegrationTest {
//..other class declarations..
@Container
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:17.2")
.withDatabaseName("testdb")
.withUsername("testuser")
.withPassword("testpass");
}