Controller Integration Testing
Relevant source files
The following files were used as context for generating this wiki page:
- Part1/API/src/test/java/com/example/api/EmployeeControllerTest.java
- Part1/API/src/main/java/com/example/api/controller/EmployeeController.java
- Part1/API/src/main/java/com/example/api/controller/DepartmentController.java
- Part1/API/src/main/java/com/example/api/controller/AssignmentController.java
- Part1/API/src/main/java/com/example/api/exception/GlobalExceptionHandler.java
- Part1/API/src/main/resources/api-spec.yaml
Introduction
Controller integration testing in the SafeDispatch HQ Administration API ensures that the REST endpoints correctly interact with the underlying service and repository layers while adhering to defined business rules and security constraints. This testing phase validates the request handling, response status codes, and data integrity across the EmployeeController, DepartmentController, and AssignmentController.
The primary objective is to verify that API operations—such as creating employees, promoting staff based on eligibility, and managing department assignments—behave as expected under both successful and error conditions. This includes validating exception handling for scenarios like missing resources (404 Not Found) or invalid business logic (400 Bad Request).
Sources: Part1/API/src/test/java/com/example/api/EmployeeControllerTest.java, Part1/API/src/main/resources/api-spec.yaml
Test Architecture and Environment
The testing suite utilizes JUnit 5 and the Mockito framework to isolate controllers from their database dependencies. By using @ExtendWith(MockitoExtension.class), the tests mock repositories like EmployeeRepository and DepartmentRepository to simulate data persistence without a live database.
Core Testing Components
- Mocking:
@Mockis used to create stubs forEmployeeRepositoryandDepartmentRepository. - Injection:
@InjectMocksinstantiates the controllers and injects the mocked repositories into them. - Assertions: Standard JUnit assertions (e.g.,
assertEquals,assertThrows) verify the output and behavior of controller methods.
The diagram shows the relationship between the test class, the controllers under test, and the mocked dependencies.
Sources: Part1/API/src/test/java/com/example/api/EmployeeControllerTest.java:23-35
Employee Management Testing
Testing for EmployeeController focuses on CRUD operations and specific business logic like employee promotion.
Promotion Eligibility Logic
The system enforces a rule where employees must have started more than 6 months ago to be eligible for a 10% salary increase and a role update to "Senior". Tests validate:
- Eligible Promotion: Verifies salary increases by 10%.
- Ineligible Promotion: Verifies a
400 Bad Requestis thrown if the start date is less than 6 months ago or null. - Resource Missing: Verifies a
404 Not Foundwhen attempting to promote a non-existent ID.
The flowchart illustrates the validation logic executed within the promoteEmployee endpoint.
Sources: Part1/API/src/main/java/com/example/api/controller/EmployeeController.java:73-90, Part1/API/src/test/java/com/example/api/EmployeeControllerTest.java:78-124
Endpoint Validation Summary
| Endpoint | Method | Expected Status | Description |
|---|---|---|---|
/api/employees | GET | 200 OK | Returns all employees or filtered by department. |
/api/employees/{id} | GET | 200 OK / 404 | Returns specific employee details. |
/api/employees | POST | 201 Created | Creates a new employee record. |
/api/employees/{id}/promote | PUT | 200 OK / 400 | Promotes employee based on tenure. |
Sources: Part1/API/src/main/resources/api-spec.yaml:104-245, Part1/API/src/main/java/com/example/api/controller/EmployeeController.java:24-60
Department and Assignment Testing
Testing for department management ensures data integrity, specifically regarding financial constraints and relational mapping.
Department Constraints
The DepartmentController validates that any new department has a positive budget. Tests specifically target the createDepartment method to ensure:
- Budgets of
0or negative values result in anIllegalArgumentException. - Valid departments are correctly persisted via the repository.
Assignment Logic
Assignments link employees to departments with specific roles and access levels. Testing ensures that the AssignmentController correctly validates the existence of both the employee and the department before creating the link.
The sequence diagram details the cross-repository validation required for creating an assignment.
Sources: Part1/API/src/main/java/com/example/api/controller/AssignmentController.java:34-50, Part1/API/src/test/java/com/example/api/EmployeeControllerTest.java:180-205
Global Exception Handling
A critical part of integration testing is verifying that the GlobalExceptionHandler translates Java exceptions into the correct HTTP responses for the API client.
- ResponseStatusException: Mapped to its specific status (e.g., 404 for "Employee not found").
- IllegalArgumentException: Mapped to
400 Bad Request(e.g., for budget validation). - MethodArgumentNotValidException: Mapped to
400 Bad Requestfor Bean Validation failures like@Emailor@NotNull.
Sources: Part1/API/src/main/java/com/example/api/exception/GlobalExceptionHandler.java:14-36
Summary
Controller Integration Testing within this project bridges the gap between raw business logic and the external REST interface. By mocking repository layers, the tests focus on the orchestration of requests, tenure-based promotion logic, and the enforcement of data constraints (like positive budgets). This ensures the API remains robust, providing informative error codes and maintaining data consistency across employees, departments, and assignments.