Coverage & Quality
Purpose: Document JaCoCo coverage configuration, thresholds, and quality metrics for StockEase tests.
Table of Contents
- JaCoCo Overview
- Coverage Thresholds
- Coverage Matrix
- Running Coverage Reports
- Must-Cover Classes
- Quality Gates
- Related Documentation
JaCoCo Overview
What is JaCoCo?
JaCoCo (Java Code Coverage) measures which lines of code are executed during tests.
Production Code
βββ Line A: Executed β
βββ Line B: Executed β
βββ Line C: NOT executed β
Coverage = 2/3 = 66.7%
Coverage Types
| Type | Definition | Example |
|---|---|---|
| Line Coverage | % of lines executed | 70 out of 100 lines = 70% |
| Branch Coverage | % of if/else paths taken | 30 out of 50 branches = 60% |
| Method Coverage | % of methods called | 45 out of 50 methods = 90% |
| Class Coverage | % of classes tested | 8 out of 10 classes = 80% |
Current JaCoCo Status in StockEase
- Status: Ready to configure (not yet in pom.xml)
- Recommendation: Add to pom.xml before first CI/CD build
- Target: 70% line, 60% branch for controllers
Coverage Thresholds
Recommended Thresholds by Layer
| Component | Line | Branch | Why |
|---|---|---|---|
| Controllers | 80% | 70% | Critical path, test all paths |
| Services | 75% | 60% | Business logic, test main cases |
| Utilities | 80% | 75% | Reusable code, cover edge cases |
| Repositories | 60% | 50% | Usually mocked; @DataJpaTest coverage if needed |
| Entities | 40% | 30% | Mostly getters/setters |
| Config | 50% | 40% | Usually tested indirectly |
Proposed StockEase Thresholds
<!-- pom.xml (to be added) -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<includes>
<include>com.stocks.stockease.controller.*</include>
</includes>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.70</minimum> <!-- 70% -->
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.60</minimum> <!-- 60% -->
</limit>
</limits>
</rule>
</rules>
</configuration>
</plugin>Coverage Matrix
Current Coverage (Estimated)
| Package | Classes | Tests | Coverage | Status |
|---|---|---|---|---|
| Controller | 2 | 8 | ~85% | β Good |
| Security | 2 | ~7* | ~75% | β Good |
| Config | 1 | 1 | ~50% | π‘ Adequate |
| Model | 2 | 0 | ~30% | β Low |
| Repository | 1 | 0 | ~0% | β Low (Mocked) |
| TOTAL | 8 | 9 | ~55% | π‘ Acceptable |
*Security tests distributed across controller tests
Coverage by Feature
| Feature | Component | Coverage | Status |
|---|---|---|---|
| Authentication | AuthController, JwtUtil | 85% | β Tested |
| Product CRUD | ProductController | 90% | β Tested |
| Authorization | SecurityConfig, Filters | 70% | β Tested |
| Validation | Controllers | 60% | π‘ Partial |
| Entities | Product, User | 30% | β Minimal |
| Repositories | ProductRepository | 0% | β Mocked |
Running Coverage Reports
Generate Coverage Report
cd backend
# Option 1: Full test + coverage
mvn clean test jacoco:report
# Option 2: Skip unit tests, only coverage
mvn clean jacoco:report
# Option 3: With specific JDK
mvn -DjavaHome=/path/to/jdk17 clean test jacoco:reportView HTML Report
# After running above, open:
open backend/target/site/jacoco/index.html
# On Windows:
start backend\target\site\jacoco\index.html
# Report shows:
# - Overall coverage percentage
# - Package breakdown
# - Class coverage details
# - Line-by-line code view (green/yellow/red)Report Structure
index.html β Start here (overall stats)
βββ com.stocks.stockease.controller
β βββ AuthController.java.html
β βββ ProductController.java.html
βββ com.stocks.stockease.security
β βββ JwtUtil.java.html
β βββ SecurityConfig.java.html
βββ com.stocks.stockease.model
β βββ Product.java.html
β βββ User.java.html
βββ [more packages...]
Report Interpretation
Line Coverage: 87% (261/300)
ββ Green (261): Executed by tests β
ββ Yellow (20): Partially executed (one branch missed)
ββ Red (19): Not executed by tests β
Branch Coverage: 72% (80/111)
ββ All branches of if/else tested
ββ Some edge cases not covered
Must-Cover Classes
Critical Path (100% Coverage Target)
These classes implement core business logic and must be thoroughly tested:
| Class | File | Why Critical | Target |
|---|---|---|---|
AuthController |
controller/ | Handles all auth flows | 95% |
ProductController |
controller/ | Handles all product ops | 95% |
JwtUtil |
security/ | Token validation, generation | 90% |
SecurityConfig |
security/ | Spring Security setup | 80% |
Standard Path (70-80% Coverage Target)
Implement business logic but less critical:
| Class | File | Why | Target |
|---|---|---|---|
UserRepository |
repository/ | Data access (mocked) | 60%* |
ProductRepository |
repository/ | Data access (mocked) | 60%* |
AuthenticationManager |
config/ | Spring config | 70% |
*Repository coverage through integration tests, not unit tests
Optional (40-60% Coverage Target)
Helper classes, models, or less critical paths:
| Class | File | Why | Target |
|---|---|---|---|
Product |
model/ | Entity (getters/setters) | 50% |
User |
model/ | Entity (getters/setters) | 50% |
HealthController |
controller/ | Non-critical endpoint | 60% |
Quality Gates
Build-Time Gates (CI/CD)
# In GitHub Actions (.github/workflows/ci-build.yml)
- name: Run tests with coverage
run: mvn clean test jacoco:report
- name: Check coverage thresholds
run: |
# Fail if coverage < 70% for controllers
mvn jacoco:check -Djacoco.minimum=0.70Coverage-Based Decisions
| Coverage | Decision | Action |
|---|---|---|
| < 50% | Critical | β Block merge, add tests |
| 50-70% | Warning | β οΈ Request review, optional tests |
| 70-85% | Good | β Approve, monitor drift |
| 85%+ | Excellent | β Approve, consider diminishing returns |
Regression Prevention
# Compare coverage to previous build
mvn jacoco:report
# If coverage decreased:
# - Don't merge without explanation
# - Add tests for new/changed codeImproving Coverage
Step 1: Identify Uncovered Code
# After running: mvn clean test jacoco:report
# Open: backend/target/site/jacoco/index.html
# Click on class β scroll to red linesStep 2: Write Tests for Uncovered Lines
// β Uncovered code
if (product.getQuantity() < 5) {
// This branch never tested
log.warn("Low stock warning");
}
// β
Add test
@Test
void testLowStockWarning() {
Product product = new Product("Test", 2, 100.0);
mockMvc.perform(get("/api/products/1"))
.andExpect(status().isOk());
// Now warning branch is covered
}Step 3: Run Verification
mvn clean test jacoco:report
# Check index.html to confirm coverage improvedStep 4: Set Coverage Gate (Prevent Regression)
<!-- pom.xml -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.70</minimum>
</limit>
</limits>
<excludes>
<exclude>**/config/AppConfig.class</exclude>
<exclude>**/model/*.class</exclude>
</excludes>
</rule>
</rules>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal> <!-- Fails build if threshold not met -->
</goals>
</execution>
</executions>
</plugin>Coverage Reports in CI/CD
Artifact Upload (GitHub Actions)
# .github/workflows/ci-build.yml
- name: Upload coverage report
uses: actions/upload-artifact@v3
if: always()
with:
name: jacoco-report
path: backend/target/site/jacoco/
- name: Publish to GitHub Pages
uses: actions/upload-pages-artifact@v2
with:
path: backend/target/site/jacoco/View in GitHub Actions
- Go to Actions β Recent build
- Scroll to βArtifactsβ section
- Download
jacoco-report.zip - Extract and open
index.htmlin browser
Publish to GitHub Pages
# After merge to main, coverage report auto-publishes to:
# https://keglev.github.io/stockease/coverage/
# Link in README.md:
# [](https://keglev.github.io/stockease/coverage/)Coverage Best Practices
β Do
- β Test happy path (main flow)
- β Test error cases (exceptions)
- β Test authorization checks
- β Test boundary conditions (empty lists, null values)
- β Aim for 70%+ coverage on critical paths
- β Use mutation testing to validate test quality
β Donβt
- β Aim for 100% coverage (diminishing returns)
- β Test generated code (getters/setters on entities)
- β Test Spring Framework code (security, config)
- β Write tests just to increase coverage (test quality matters)
- β Lower thresholds to pass build (fix code instead)
Quality Metrics Dashboard (Future)
Proposed Monitoring
Coverage Trends (Last 30 Days)
Line Coverage: 70% β 72% β (Good)
Branch Coverage: 60% β 62% β (Good)
Test Count: 9 β 12 β (Growing)
Execution Time: 25s β 28s β (Acceptable)
Top Uncovered Code:
1. Product.calculateDiscount() β 0% (unused feature)
2. AuthService.refreshToken() β 0% (future)
3. ValidationUtil.validateEmail() β 0% (imported, unused)
Related Documentation
Testing Fundamentals
- Testing Strategy β Coverage goals
- Test Pyramid β Distribution of test types
- CI Pipeline Tests β Coverage in CI/CD
Implementation
- Spring Slices β How to write testable code
- Controller Integration Tests β HTTP test examples
- Security Tests β Authorization coverage
Main Architecture
- Testing Architecture β Entry point
- Backend Architecture β Code being measured
- Deployment β Publishing reports
Last Updated: October 31, 2025
Version: 1.0
Status: β
Ready for implementation