This project demonstrates Integration Testing in Spring Boot using a simple Product CRUD API with H2 in-memory database.
The tests verify the REST API endpoints end-to-end (Controller → Service → Repository → Database).
- Java 21
- Spring Boot 3.5.6
- Spring Data JPA
- H2 Database (in-memory)
- JUnit 5
- RestTemplate for HTTP requests
- @Sql for test data setup/cleanup
The integration tests cover full CRUD functionality:
-
Create (POST)
testAddProduct()
→ Adds a product viaPOST /products
and validates DB state.
-
Read All (GET)
testGetProducts()
→ Fetches all products withGET /products
.
-
Read by ID (GET)
testFindProductById()
→ Fetches product details by ID usingGET /products/{id}
.
-
Update (PUT)
testUpdateProduct()
→ Updates an existing product withPUT /products/update/{id}
.
-
Delete (DELETE)
testDeleteProduct()
→ Deletes a product withDELETE /products/delete/{id}
.
-
@SpringBootTest(webEnvironment = RANDOM_PORT)
Loads the full Spring application context and starts the server on a random port. -
@LocalServerPort
Injects the server port for real HTTP requests during tests. -
RestTemplate
in tests
Sends real HTTP requests to the running Spring Boot app. -
@Sql
annotation
Preloads and cleans test data before/after each test method. -
H2 In-Memory Database
Provides a lightweight, isolated database for testing. -
Assertions in JUnit 5
assertEquals
,assertNotNull
,assertAll
for grouped assertions.
- Ensures the entire flow works correctly (Controller → Service → Repository → DB).
- Validates real database operations instead of mocked calls.
- Guarantees endpoints behave as expected in production-like scenarios.