nbl.test-coverage
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTest Coverage Skill
Test Coverage Skill
分析测试覆盖率,识别缺口,生成缺失的测试以达到80%+覆盖率。
Analyze test coverage, identify gaps, and generate missing tests to achieve over 80% coverage.
激活时机
Activation Timing
- 用户请求测试覆盖率分析
- 提升测试覆盖率
- 编写缺失的测试
- Users request test coverage analysis
- Improve test coverage
- Write missing tests
步骤0:识别测试范围(关键)
Step 0: Identify Test Scope (Critical)
✅ 必须测试(100%覆盖目标)
✅ Mandatory Testing (100% Coverage Target)
| 类型 | 包路径模式 | 测试方式 | 原因 |
|---|---|---|---|
| Service层 | | 单元测试 | 核心业务逻辑 |
| Manager层 | | 单元测试 | 外部集成逻辑 |
| Controller层 | | 集成测试 | API契约验证 |
| 工具类 | | 单元测试 | 跨服务复用 |
| 财务计算 | 所有含计算逻辑的类 | 单元测试 | 高风险 |
| 认证授权 | | 单元测试 | 安全关键 |
| Type | Package Path Pattern | Testing Method | Reason |
|---|---|---|---|
| Service Layer | | Unit Test | Core business logic |
| Manager Layer | | Unit Test | External integration logic |
| Controller Layer | | Integration Test | API contract verification |
| Utility Classes | | Unit Test | Cross-service reuse |
| Financial Calculation | All classes with calculation logic | Unit Test | High risk |
| Authentication & Authorization | | Unit Test | Security-critical |
🚫 排除测试(不计入覆盖率)
🚫 Excluded from Testing (Not counted in coverage)
| 类型 | 包路径模式 | 原因 |
|---|---|---|
| Entity类 | | 纯数据载体,无逻辑 |
| DTO类 | | 纯数据载体,通过其他测试间接覆盖 |
| Req/Resp/Query | | POJO,通过Controller测试间接覆盖 |
| 枚举类 | | 纯定义,无业务逻辑 |
| 常量类 | | 仅静态常量 |
| 配置类 | | Spring管理,无业务逻辑 |
| Mapper层 | | 框架生成实现,通过集成测试间接覆盖 |
| MyBatis拦截器 | | MyBatis插件框架,通过集成测试覆盖 |
| 定时任务 | | 通过集成测试覆盖 |
| 启动类 | | 无测试价值 |
| 事件类 | | 纯数据载体,无业务逻辑 |
| 事件监听器 | | Spring框架管理,通过集成测试覆盖 |
| 消息队列监听器 | | MQ框架管理,需要真实环境 |
| Type | Package Path Pattern | Reason |
|---|---|---|
| Entity Classes | | Pure data carrier, no logic |
| DTO Classes | | Pure data carrier, indirectly covered by other tests |
| Req/Resp/Query | | POJO, indirectly covered by Controller tests |
| Enum Classes | | Pure definition, no business logic |
| Constant Classes | | Only static constants |
| Configuration Classes | | Managed by Spring, no business logic |
| Mapper Layer | | Framework-generated implementation, indirectly covered by integration tests |
| MyBatis Interceptors | | MyBatis plugin framework, covered by integration tests |
| Scheduled Jobs | | Covered by integration tests |
| Startup Classes | | No testing value |
| Event Classes | | Pure data carrier, no business logic |
| Event Listeners | | Managed by Spring framework, covered by integration tests |
| Message Queue Listeners | | Managed by MQ framework, requires real environment |
JaCoCo排除配置
JaCoCo Exclusion Configuration
在中配置排除:
pom.xmlxml
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<!-- Entity/DTO/VO -->
<exclude>**/model/entity/**</exclude>
<exclude>**/model/dto/**</exclude>
<exclude>**/model/enums/**</exclude>
<exclude>**/model/request/**</exclude>
<exclude>**/model/response/**</exclude>
<exclude>**/model/query/**</exclude>
<!-- Mapper层 -->
<exclude>**/mapper/**</exclude>
<!-- MyBatis拦截器 -->
<exclude>**/*Interceptor.class</exclude>
<!-- 事件类 -->
<exclude>**/*Event.class</exclude>
<!-- 监听器 -->
<exclude>**/listener/**</exclude>
<!-- 消息队列 -->
<exclude>**/messagequeue/**</exclude>
<exclude>**/mq/**</exclude>
<!-- 配置类 -->
<exclude>**/config/**</exclude>
<!-- 常量类 -->
<exclude>**/constants/**</exclude>
<!-- 启动类 -->
<exclude>**/*Application.class</exclude>
</excludes>
</configuration>
</plugin>Configure exclusions in :
pom.xmlxml
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<!-- Entity/DTO/VO -->
<exclude>**/model/entity/**</exclude>
<exclude>**/model/dto/**</exclude>
<exclude>**/model/enums/**</exclude>
<exclude>**/model/request/**</exclude>
<exclude>**/model/response/**</exclude>
<exclude>**/model/query/**</exclude>
<!-- Mapper层 -->
<exclude>**/mapper/**</exclude>
<!-- MyBatis拦截器 -->
<exclude>**/*Interceptor.class</exclude>
<!-- 事件类 -->
<exclude>**/*Event.class</exclude>
<!-- 监听器 -->
<exclude>**/listener/**</exclude>
<!-- 消息队列 -->
<exclude>**/messagequeue/**</exclude>
<exclude>**/mq/**</exclude>
<!-- 配置类 -->
<exclude>**/config/**</exclude>
<!-- 常量类 -->
<exclude>**/constants/**</exclude>
<!-- 启动类 -->
<exclude>**/*Application.class</exclude>
</excludes>
</configuration>
</plugin>步骤1:检测测试框架
Step 1: Detect Test Framework
| 标识 | 覆盖率命令 |
|---|---|
| |
| |
| Indicator | Coverage Command |
|---|---|
| |
| |
步骤2:分析覆盖率报告
Step 2: Analyze Coverage Report
- 运行覆盖率命令
- 解析输出(target/site/jacoco/jacoco.xml 或终端输出)
- 过滤排除类后,列出低于80%覆盖率的文件,按最差优先排序
- 对于每个覆盖率不足的文件,识别:
- 未测试的方法
- 缺失的分支覆盖(if/else、switch、异常路径)
- 死代码(膨胀分母)
- Run the coverage command
- Parse the output (target/site/jacoco/jacoco.xml or terminal output)
- After filtering excluded classes, list files with coverage lower than 80%, sorted by lowest coverage first
- For each file with insufficient coverage, identify:
- Untested methods
- Missing branch coverage (if/else, switch, exception paths)
- Dead code (inflates denominator)
步骤3:生成缺失测试
Step 3: Generate Missing Tests
测试优先级
Test Priority
| 优先级 | 测试类型 | 覆盖目标 |
|---|---|---|
| P0 | 成功路径 | 核心功能使用有效输入 |
| P0 | 异常处理 | 无效输入、业务异常 |
| P1 | 边界案例 | 空集合、null、边界值 |
| P1 | 分支覆盖 | 每个if/else、switch case |
| Priority | Test Type | Coverage Target |
|---|---|---|
| P0 | Happy Path | Core functionality with valid input |
| P0 | Exception Handling | Invalid input, business exceptions |
| P1 | Edge Cases | Empty collections, null, boundary values |
| P1 | Branch Coverage | Each if/else, switch case |
测试生成规则
Test Generation Rules
- Service/Manager/Utils:单元测试放在
src/test/java/.../ - Controller:集成测试放在
src/test/java/.../controller/ - 使用项目现有的测试模式(import风格、断言库、Mock方式)
- Mock外部依赖(数据库、外部API、文件系统)
- 每个测试应该独立 — 测试之间不共享可变状态
- Service/Manager/Utils: Unit tests are placed in
src/test/java/.../ - Controller: Integration tests are placed in
src/test/java/.../controller/ - Follow existing test patterns of the project (import style, assertion library, Mock approach)
- Mock external dependencies (databases, external APIs, file systems)
- Each test should be independent — no mutable state shared between tests
步骤4:验证
Step 4: Verification
- 运行完整测试套件 — 所有测试必须通过
- 重新运行覆盖率 — 验证改进
- 如果仍低于80%,重复步骤3处理剩余缺口
- Run the full test suite — all tests must pass
- Rerun coverage calculation — verify improvements
- If coverage is still below 80%, repeat step 3 to address remaining gaps
步骤5:报告
Step 5: Report
覆盖率报告(已排除Entity/DTO/Mapper/Config类)
──────────────────────────────────────────────────
文件 之前 之后 状态
──────────────────────────────────────────────────
DiscountServiceImpl.java 45% 88% ✅
OrderServiceImpl.java 32% 82% ✅
OrderManagerImpl.java 55% 85% ✅
──────────────────────────────────────────────────
总体覆盖率: 46% 87% ✅Coverage Report (Entity/DTO/Mapper/Config classes excluded)
──────────────────────────────────────────────────
File Before After Status
──────────────────────────────────────────────────
DiscountServiceImpl.java 45% 88% ✅
OrderServiceImpl.java 32% 82% ✅
OrderManagerImpl.java 55% 85% ✅
──────────────────────────────────────────────────
Overall Coverage: 46% 87% ✅覆盖率目标
Coverage Target
| 代码类型 | 目标覆盖率 | 测试方式 |
|---|---|---|
| 财务计算、认证、安全 | 100% | 单元测试 |
| Service/Manager业务逻辑 | 80%+ | 单元测试 |
| Controller层 | 80%+ | 集成测试 |
| 工具类 | 80%+ | 单元测试 |
| Code Type | Target Coverage | Testing Method |
|---|---|---|
| Financial calculation, authentication, security | 100% | Unit Test |
| Service/Manager business logic | 80%+ | Unit Test |
| Controller Layer | 80%+ | Integration Test |
| Utility classes | 80%+ | Unit Test |