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-postgresql
You can run the application using Maven:
./mvnw spring-boot:run
Or build and run the JAR file:
./mvnw clean package
java -jar target/test-container-postgresql-0.0.1-SNAPSHOT.jar
The project includes a compose.yaml
file for running the PostgreSQL database:
docker-compose up -d
The application provides the following RESTful API endpoints:
GET /api/posts
- Get all postsGET /api/posts/{id}
- Get a post by IDPOST /api/posts
- Create a new postPUT /api/posts/{id}
- Update an existing postDELETE /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/posts
curl -X GET http://localhost:8080/api/posts/1
curl -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/1
The 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-example
The application uses a simple Post
model with the following fields:
id
(Integer) - Primary keyuserId
(Integer) - User ID associated with the posttitle
(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=PostRepositoryTest
Tests the controller layer with a real PostgreSQL database using Testcontainers.
./mvnw test -Dtest=PostControllerTest
Tests the entire application with a real PostgreSQL database using Testcontainers.
./mvnw test -Dtest=SpringBootTestContainerPostgresqlApplicationTests
src/main/java
- Java source codecontroller
- REST controllersmodel
- Data modelsrepository
- Data access layerexception
- Custom exceptions
src/main/resources
- Application resourcesdata/posts.json
- Initial data for the applicationschema.sql
- Database schema definition
src/test/java
- Test source code
This project is open source and available under the MIT License.
Hendi Santika
- Email: hendisantika@gmail.com
- Telegram: @hendisantika34