Skip to content

Vivekbhand09/SpringBoot-Integration-Testing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧪 Spring Boot Integration Testing – CRUD Example

Java Spring Boot JUnit5 Maven H2 Database Code Coverage Integration Test

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).


🚀 Tech Stack

  • 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

📂 Features Tested

The integration tests cover full CRUD functionality:

  1. Create (POST)

    • testAddProduct() → Adds a product via POST /products and validates DB state.
  2. Read All (GET)

    • testGetProducts() → Fetches all products with GET /products.
  3. Read by ID (GET)

    • testFindProductById() → Fetches product details by ID using GET /products/{id}.
  4. Update (PUT)

    • testUpdateProduct() → Updates an existing product with PUT /products/update/{id}.
  5. Delete (DELETE)

    • testDeleteProduct() → Deletes a product with DELETE /products/delete/{id}.

🔑 Key Concepts Demonstrated

  • @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.

⚡ Why This Matters

  • 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages