This project demonstrates Test-Driven Development (TDD) practices using Spring Boot, focusing on building a RESTful API for managing user profiles. The project utilizes modern testing practices including integration tests with a development database.
- Java
- Spring Boot
- Spring Data JPA
- MySQL
- JUnit 5
- Mockito
- AssertJ
The project follows a layered architecture:
src/
├── main/java/com/spring/tdd/
│ ├── controller/ # REST controllers
│ ├── service/ # Business logic layer
│ ├── repository/ # Data access layer
│ ├── model/ # Domain entities
│ └── dto/ # Data Transfer Objects
└── test/java/com/spring/tdd/
├── controller/ # Controller tests
└── service/ # Service layer tests
The project implements different types of tests:
- Unit Tests: Using Mockito for service layer testing
- Integration Tests: Using a development database for integration testing
- API Tests: Using MockMvc for testing REST endpoints
- POST
/api/profiles - Request Body:
{ "name": "string", "age": number } - Response: 201 Created
{ "id": number, "name": "string", "age": number }
- GET
/api/profiles/{id} - Response: 200 OK
{ "id": number, "name": "string", "age": number }
- GET
/api/profiles - Response: 200 OK
[ { "id": number, "name": "string", "age": number } ]
- Java 17 or higher
- MySQL Database
- Maven
./mvnw test./mvnw spring-boot:runThe application uses MySQL as its database. Make sure you have a MySQL instance running and configure the following properties in your application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/profiledb
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.jpa.hibernate.ddl-auto=updateMake sure to create the database before running the application:
CREATE DATABASE profiledb;This project follows Test-Driven Development (TDD) principles:
- Write a failing test
- Write the minimum code to make the test pass
- Refactor the code while keeping the tests green
The API implements proper error handling:
- 404 Not Found: When requesting a non-existent profile
- 400 Bad Request: When submitting invalid profile data
- 201 Created: When successfully creating a profile
- 200 OK: When successfully retrieving profile(s)