Testing Strategy & Automation
Relevant source files
The following files were used as context for generating this wiki page:
- src/test/java/org/example/coding_convention/project/ProjectControllerTest.java
- src/test/java/org/example/coding_convention/project/ProjectServiceTest.java
- src/test/java/org/example/coding_convention/CodingConventionApplicationTests.java
- src/main/resources/data.sql
- src/main/java/org/example/coding_convention/project/service/ProjectService.java
- src/main/java/org/example/coding_convention/test/TestController.java
The PhanesEditor backend employs a multi-layered testing strategy designed to ensure the reliability of its real-time collaborative features. The strategy encompasses unit testing for business logic, integration testing for API endpoints, and database state management to provide a consistent environment for automated verification.
The testing scope primarily focuses on the core project management, member access control, and file handling modules. By separating concerns between service-layer logic and controller-layer communication, the system maintains high code quality and facilitates rapid identification of regressions during development.
Sources: README.md, src/test/java/org/example/coding_convention/project/ProjectServiceTest.java:18-20
Unit Testing Logic
Unit tests are implemented to verify the isolated business logic within the service layer. These tests utilize the Mockito framework to mock repository dependencies, allowing for deterministic testing of service methods without requiring a live database or network connection.
Service Layer Verification
The ProjectServiceTest class demonstrates the pattern of mocking ProjectRepository to validate project creation logic. This ensures that the conversion from Request DTOs to Entities and the subsequent saving process work correctly.
The diagram above illustrates the dependency injection and mocking flow used during service-level unit testing.
Sources: src/test/java/org/example/coding_convention/project/ProjectServiceTest.java:23-28, src/test/java/org/example/coding_convention/project/ProjectServiceTest.java:34-60
Integration Testing & API Automation
Integration tests are used to verify the interaction between the application layers and the web environment. These tests run within a Spring Boot context and perform actual HTTP requests to local endpoints.
Controller Integration
The ProjectControllerTest class utilizes TestRestTemplate to perform integration testing on REST endpoints. It validates the full request-response cycle, including JSON serialization/deserialization and HTTP status codes.
| Test Case | Method | Endpoint | Description |
|---|---|---|---|
| Project Creation | POST | /product/register | Validates that a project can be successfully registered and returns the correct index. |
| Project Read | POST | /project/read/1 | Verifies that project details can be retrieved by ID. |
Sources: src/test/java/org/example/coding_convention/project/ProjectControllerTest.java:21-25, src/test/java/org/example/coding_convention/project/ProjectControllerTest.java:31-49, src/test/java/org/example/coding_convention/project/ProjectControllerTest.java:54-61
Testing Environment Configuration
Integration tests are configured with SpringBootTest.WebEnvironment.RANDOM_PORT to avoid port conflicts during automated build processes.
Sources: src/test/java/org/example/coding_convention/project/ProjectControllerTest.java:19-20
Database Initialization for Testing
The project maintains a data.sql script that serves as a baseline for the database state during testing and development. This script handles the teardown and setup of the relational schema.
Schema Setup Flow
The automation of database initialization follows a specific order to satisfy foreign key constraints:
- Cleanup: Drops existing tables in reverse order of dependency (e.g.,
chats,files,project_memberbeforeprojectandusers). - Creation: Re-creates the schema including
users,project,files,project_member, andchats. - Seeding: Inserts sample data for users, projects, and initial file structures to support test scenarios.
This flow ensures that every test run starts with a clean, predictable data set.
Sources: src/main/resources/data.sql:6-25, src/main/resources/data.sql:110-140
Health Monitoring & Connectivity Tests
A dedicated TestController provides lightweight endpoints to verify system availability and basic connectivity without invoking complex business logic.
/health: Returns a success response to indicate the application context is up and running./test: Used for basic verification of the response wrapper (BaseResponse).
Sources: src/main/java/org/example/coding_convention/test/TestController.java:10-18
Conclusion
The testing strategy for PhanesEditor-BE combines targeted unit tests for service logic with broad integration tests for API endpoints. By utilizing Mockito for isolation and a centralized SQL seeding script for environmental consistency, the project ensures that core collaboration features—such as project creation and member management—remain stable across changes. Sources: src/test/java/org/example/coding_convention/project/ProjectServiceTest.java, src/test/java/org/example/coding_convention/project/ProjectControllerTest.java, src/main/resources/data.sql