Testing Guide
This guide covers testing practices and how to run tests for the Duit application.Table of Contents
Test Structure
Test Directory Layout
Tests are located in the standard Maven test directory:Testing Dependencies
The project includes the following testing dependencies (frompom.xml):
- JUnit 5 (Jupiter)
- Spring Boot Test
- Spring Security Test
- Mockito
- AssertJ
- Hamcrest
Running Tests
Running All Tests
Using Maven:Running Specific Test Classes
Running Specific Test Methods
Skip Tests During Build
Running Tests in IDE
IntelliJ IDEA:- Right-click on test class or method
- Select “Run ‘TestName’”
- Right-click on test class
- Select “Run As” → “JUnit Test”
Writing Tests
Unit Tests
Unit tests focus on testing individual components in isolation.Service Layer Test Example
Repository Test Example
Integration Tests
Integration tests verify that multiple components work together correctly.Controller Tests with Security
Test Best Practices
Naming Conventions
Test Classes:- Service tests:
{ClassName}Test - Integration tests:
{ClassName}IntegrationTest
- Pattern:
test{MethodName}_{Scenario}_{ExpectedResult} - Examples:
testRegisterUser_ValidData_ReturnsUsertestSaveRequest_InvalidCategory_ThrowsExceptiontestPublishRequest_DraftStatus_ChangeToPublished
AAA Pattern (Arrange-Act-Assert)
Structure tests using the AAA pattern:Test Data Management
Use test data builders:Mocking Guidelines
- Mock external dependencies - Database, external APIs
- Don’t mock the class under test - Test real implementation
- Verify important interactions - Use
verify()when needed - Use argument matchers carefully - Prefer exact values when possible
Test Coverage
Measuring Coverage
Add JaCoCo plugin topom.xml:
target/site/jacoco/index.html
Coverage Goals
- Services: Aim for 80%+ coverage
- Controllers: Test happy path and error cases
- Repositories: Test custom queries
- Utilities: Aim for 100% coverage
Manual Testing
Test User Accounts
Create test users with different roles for manual testing:Manual Testing Checklist
User Registration:- Register as USER
- Register as PROFESSIONAL
- Test with duplicate email
- Test with duplicate DNI
- Test with invalid email format
- Test password requirements
- Login with valid credentials
- Login with invalid credentials
- Logout
- Access restricted pages without auth
- Create draft request
- Create and publish request
- Edit draft request
- Publish draft request
- Unpublish request
- Cancel request
- Delete request
- View applications
- Accept application
- Reject application
- Search for requests
- Apply to request
- Edit application
- Withdraw application
- Start job
- Pause job
- Resume job
- Complete job
- Submit rating as client
- Submit rating as professional
- View rating history
- View users list
- Create category
- Edit category
- Delete category
- Toggle category status
Browser Testing
Test on multiple browsers:- Chrome/Chromium
- Firefox
- Safari (if available)
- Edge
Mobile Testing
Test responsive design:- Mobile viewport (320px - 480px)
- Tablet viewport (768px - 1024px)
- Desktop viewport (1280px+)
Continuous Integration
GitHub Actions Example
Create.github/workflows/test.yml:
Troubleshooting Tests
Common Issues
Test fails with “No bean found”:- Add
@MockBeanfor required dependencies - Use
@SpringBootTestfor full application context
- Check database is running
- Verify connection properties
- Use
@Transactionalto rollback test data
- Use
@WithMockUserannotation - Include CSRF token with
with(csrf())
- Avoid time-dependent assertions
- Don’t rely on execution order
- Clean up test data properly
Resources
Happy testing!
