A demonstration project showcasing the integration of Spring Boot with Testcontainers for PostgreSQL database testing.
This project is a RESTful API for managing blog posts, built with Spring Boot and PostgreSQL. It demonstrates how to use Testcontainers for integration testing with a real PostgreSQL database.
- Java 21 (with preview features)
- Spring Boot 3.5.0
- Spring Data JDBC
- Spring WebFlux (WebClient)
- PostgreSQL
- Testcontainers
- Docker & Docker Compose
- Maven
- Springdoc OpenAPI
- Java 21 or higher
- Docker and Docker Compose
- Maven
git clone https://github.com/hendisantika/spring-boot-test-container-postgresql.git
cd spring-boot-test-container-postgresqlYou can run the application using Maven:
./mvnw spring-boot:runOr build and run the JAR file:
./mvnw clean package
java -jar target/test-container-postgresql-0.0.1-SNAPSHOT.jarThe project includes a compose.yaml file for running the PostgreSQL database:
docker-compose up -dThe application provides the following RESTful API endpoints:
- GET /api/posts- Get all posts
- GET /api/posts/{id}- Get a post by ID
- POST /api/posts- Create a new post
- PUT /api/posts/{id}- Update an existing post
- DELETE /api/posts/{id}- Delete a post
The API documentation is available via Springdoc OpenAPI at:
http://localhost:8080/swagger-ui.html
You can interact with the API using cURL commands:
curl -X GET http://localhost:8080/api/postscurl -X GET http://localhost:8080/api/posts/1curl -X POST http://localhost:8080/api/posts \
  -H "Content-Type: application/json" \
  -d '{"userId": 1, "title": "New Post", "body": "This is a new post created with cURL"}'curl -X PUT http://localhost:8080/api/posts/1 \
  -H "Content-Type: application/json" \
  -d '{"userId": 1, "title": "Updated Post", "body": "This post was updated with cURL"}'curl -X DELETE http://localhost:8080/api/posts/1The project includes a WebClient-based HTTP client for programmatic API access. The client is implemented in the
PostClient class.
To use the HTTP client in your code:
@Autowired
private PostClient postClient;
// Get all posts
Flux<Post> posts = postClient.getAllPosts();
// Get post by ID
Mono<Post> post = postClient.getPostById(1);
// Create a new post
Post newPost = new Post(null, 1, "New Post", "This is a new post", null);
Mono<Post> createdPost = postClient.createPost(newPost);
// Update a post
Post updatedPost = new Post(1, 1, "Updated Post", "This post was updated", null);
Mono<Post> result = postClient.updatePost(1, updatedPost);
// Delete a post
Mono<Void> deleteResult = postClient.deletePost(1);The project includes an example client that demonstrates how to use the HTTP client. To run it:
./mvnw spring-boot:run -Dspring-boot.run.profiles=client-exampleThe application uses a simple Post model with the following fields:
- id(Integer) - Primary key
- userId(Integer) - User ID associated with the post
- title(String) - Post title (required)
- body(String) - Post content (required)
- version(Integer) - Version for optimistic locking
The project demonstrates several testing approaches using Testcontainers:
Tests the repository layer with a real PostgreSQL database using Testcontainers.
./mvnw test -Dtest=PostRepositoryTestTests the controller layer with a real PostgreSQL database using Testcontainers.
./mvnw test -Dtest=PostControllerTestTests the entire application with a real PostgreSQL database using Testcontainers.
./mvnw test -Dtest=SpringBootTestContainerPostgresqlApplicationTests- src/main/java- Java source code- controller- REST controllers
- model- Data models
- repository- Data access layer
- exception- Custom exceptions
 
- src/main/resources- Application resources- data/posts.json- Initial data for the application
- schema.sql- Database schema definition
 
- src/test/java- Test source code
This project is open source and available under the MIT License.
Hendi Santika
- Email: [email protected]
- Telegram: @hendisantika34