Skip to content

ProductController Test Suite

Overview

Comprehensive test suite for the ProductController REST API endpoints, covering all CRUD operations with positive and negative test scenarios.

Test Summary

Test Class: com.anr.controller.ProductControllerTest
Total Tests: 13
Passed: ✅ 13
Failed: ❌ 0
Skipped: ⏭️ 0
Success Rate: 100%

Test Coverage

1. GET /api/v1/products (List All Products)

Test Method Scenario Expected Result Status
test_getAllProducts_success() Retrieve multiple products Returns 200 OK with array of 3 products ✅ PASS
test_getAllProducts_emptyList() Retrieve when no products exist Returns 200 OK with empty array ✅ PASS

Coverage: 2 tests - ✅ Success scenario with data - ✅ Success scenario with empty list


2. GET /api/v1/products/{id} (Get Product by ID)

Test Method Scenario Expected Result Status
test_getProductById_success() Retrieve existing product Returns 200 OK with product details ✅ PASS
test_getProductById_notFound() Retrieve non-existent product Returns 404 Not Found ✅ PASS

Coverage: 2 tests - ✅ Success scenario - ✅ Not found scenario


3. POST /api/v1/products (Create New Product)

Test Method Scenario Expected Result Status
test_createProduct_success() Create valid product Returns 201 Created with product ✅ PASS
test_createProduct_nullId_badRequest() Create product with null ID Returns 400 Bad Request ✅ PASS
test_createProduct_emptyId_badRequest() Create product with empty ID Returns 400 Bad Request ✅ PASS

Coverage: 3 tests - ✅ Success scenario - ✅ Validation: null ID - ✅ Validation: empty ID


4. PUT /api/v1/products/{id} (Update Product)

Test Method Scenario Expected Result Status
test_updateProduct_success() Update existing product Returns 200 OK with updated product ✅ PASS
test_updateProduct_notFound() Update non-existent product Returns 404 Not Found ✅ PASS
test_updateProduct_idMismatch() Path ID differs from body ID Uses path ID, returns 200 OK ✅ PASS

Coverage: 3 tests - ✅ Success scenario - ✅ Not found scenario - ✅ ID mismatch handling


5. DELETE /api/v1/products/{id} (Delete Product)

Test Method Scenario Expected Result Status
test_deleteProduct_success() Delete existing product Returns 204 No Content ✅ PASS
test_deleteProduct_notFound() Delete non-existent product Returns 404 Not Found ✅ PASS
test_deleteProduct_alreadyDeleted() Delete same product twice First: 204, Second: 404 ✅ PASS

Coverage: 3 tests - ✅ Success scenario - ✅ Not found scenario - ✅ Idempotency check


Testing Approach

Framework & Tools

  • Testing Framework: JUnit 5
  • Mocking: Mockito with @MockBean
  • HTTP Testing: Spring MockMvc
  • JSON Processing: Jackson ObjectMapper
  • Test Type: Integration tests with mocked service layer

Test Structure

@SpringBootTest
@AutoConfigureMockMvc
public class ProductControllerTest {
    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private ProductService productService;

    // Test methods...
}

Key Testing Patterns

1. MockMvc for HTTP Testing

  • Simulates HTTP requests without starting a full server
  • Validates response status codes, headers, and body
  • Uses fluent API for readable test assertions

2. Service Layer Mocking

  • @MockBean to mock ProductService
  • Isolates controller logic from service implementation
  • Allows testing controller behavior independently

3. JSON Validation

  • Uses jsonPath() to validate response structure
  • Verifies field values and array lengths
  • Ensures proper JSON serialization

4. Comprehensive Assertions

.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.id").value("P001"))
.andExpect(jsonPath("$.name").value("Laptop"))

Test Data

Sample Products Used

Product("P001", "Laptop", "High-performance laptop", "1299.99")
Product("P002", "Mouse", "Wireless mouse", "29.99")
Product("P003", "Keyboard", "Mechanical keyboard", "89.99")
Product("P004", "Monitor", "4K Monitor", "499.99")

Code Quality Metrics

Test Coverage by Endpoint

  • GET /api/v1/products: 2 tests (100% coverage)
  • GET /api/v1/products/{id}: 2 tests (100% coverage)
  • POST /api/v1/products: 3 tests (100% coverage)
  • PUT /api/v1/products/{id}: 3 tests (100% coverage)
  • DELETE /api/v1/products/{id}: 3 tests (100% coverage)

Scenarios Covered

✅ Happy Path: All successful operations
✅ Error Handling: 404 Not Found scenarios
✅ Validation: Bad request scenarios
✅ Edge Cases: Empty lists, ID mismatches, idempotency
✅ HTTP Status Codes: 200, 201, 204, 400, 404
✅ Content Type: JSON request/response validation

Running the Tests

Run all ProductController tests

./gradlew test --tests ProductControllerTest

Run specific test

./gradlew test --tests ProductControllerTest.test_getAllProducts_success

Run all tests with detailed output

./gradlew test --tests ProductControllerTest --info

View test report

open build/reports/tests/test/index.html

Test Results Location

  • XML Results: build/test-results/test/TEST-com.anr.controller.ProductControllerTest.xml
  • HTML Report: build/reports/tests/test/index.html
  • Console Output: Displayed during test execution

Integration with CI/CD

These tests are designed to run in CI/CD pipelines: - ✅ Fast execution (< 1 second) - ✅ No external dependencies - ✅ Deterministic results - ✅ Clear failure messages - ✅ Compatible with Gradle test task

Best Practices Demonstrated

  1. Descriptive Test Names: Clear indication of what is being tested
  2. Arrange-Act-Assert: Structured test organization
  3. Independent Tests: No dependencies between tests
  4. Mock Isolation: Service layer properly mocked
  5. Comprehensive Coverage: All endpoints and scenarios covered
  6. Proper HTTP Semantics: Correct status codes and methods
  7. JSON Validation: Response structure verification
  8. Edge Case Testing: Boundary conditions tested

Future Enhancements

Potential additions to the test suite: - [ ] Performance tests for bulk operations - [ ] Security tests (authentication/authorization) - [ ] Concurrent request handling tests - [ ] Request validation tests (malformed JSON) - [ ] Integration tests with real database - [ ] Contract testing with Pact or Spring Cloud Contract


Test Suite Created: October 22, 2025
Last Run: October 22, 2025
Status: ✅ All Tests Passing
Maintainer: Development Team