Overview
The User Management System uses JUnit 5, Mockito, and Spring Boot Test for comprehensive testing coverage. Tests are organized into unit and integration tests, with JaCoCo providing code coverage reports.Running Tests
Run all tests
Execute the complete test suite using Maven:This command runs all unit and integration tests and generates a JaCoCo coverage report.
Test Structure
Unit Tests
Unit tests verify individual components in isolation using mocks. Located insrc/test/java/.../unit/
UserServiceImplTest - Service Layer Tests
UserServiceImplTest - Service Layer Tests
Tests the core user service business logic with mocked dependencies.Location:
src/test/java/dev/juanJe/userManagementSystem/unit/service/UserServiceImplTest.javaKey test cases:- User registration with valid data
- Duplicate user detection (username/email)
- User authentication with valid credentials
- Authentication failure with invalid credentials
- User info retrieval
- Password verification
- List all users
JwtUtilTest - JWT Token Tests
JwtUtilTest - JWT Token Tests
Tests JWT token generation and validation.Location:
src/test/java/dev/juanJe/userManagementSystem/unit/security/JwtUtilTest.javaKey test cases:- Token generation with username and role
- Token validation and claims extraction
- Invalid token handling
ErrorResponseDTOTest - DTO Tests
ErrorResponseDTOTest - DTO Tests
Tests the error response data structure.Location:
src/test/java/dev/juanJe/userManagementSystem/unit/dto/ErrorResponseDTOTest.javaKey test cases:- Constructor initialization
- Automatic timestamp generation
- Error list handling
Integration Tests
Integration tests verify component interactions. Located insrc/test/java/.../integration/
JwtAuthenticationFilterTest - Security Filter Tests
JwtAuthenticationFilterTest - Security Filter Tests
Tests the JWT authentication filter in the security chain.Location:
src/test/java/dev/juanJe/userManagementSystem/integration/security/JwtAuthenticationFilterTest.javaKey test cases:- Authentication with valid JWT token
- Request handling without token
- Security context population
JaCoCo Coverage Reports
JaCoCo is configured in the Maven build to automatically generate coverage reports.Configuration
The JaCoCo plugin is configured inpom.xml:108-125:
Reading Coverage Reports
The HTML report shows:- Line coverage: Percentage of code lines executed
- Branch coverage: Percentage of conditional branches tested
- Method coverage: Percentage of methods invoked
- Class coverage: Percentage of classes tested
Coverage reports are generated automatically during
mvn test and are located in target/site/jacoco/Test Dependencies
Key testing dependencies frompom.xml:
Best Practices
Use descriptive test names
Use
@DisplayName annotations to clearly describe what each test verifies:Running Tests in CI/CD
For continuous integration pipelines:The
mvn package command automatically runs all tests before creating the JAR file.