nbl.test-coverage

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Test 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层
**/service/impl/*Impl.java
单元测试核心业务逻辑
Manager层
**/service/manager/impl/*Impl.java
单元测试外部集成逻辑
Controller层
**/controller/*.java
集成测试API契约验证
工具类
**/utils/*.java
(含复杂逻辑)
单元测试跨服务复用
财务计算所有含计算逻辑的类单元测试高风险
认证授权
**/security/**/*.java
单元测试安全关键
TypePackage Path PatternTesting MethodReason
Service Layer
**/service/impl/*Impl.java
Unit TestCore business logic
Manager Layer
**/service/manager/impl/*Impl.java
Unit TestExternal integration logic
Controller Layer
**/controller/*.java
Integration TestAPI contract verification
Utility Classes
**/utils/*.java
(with complex logic)
Unit TestCross-service reuse
Financial CalculationAll classes with calculation logicUnit TestHigh risk
Authentication & Authorization
**/security/**/*.java
Unit TestSecurity-critical

🚫 排除测试(不计入覆盖率)

🚫 Excluded from Testing (Not counted in coverage)

类型包路径模式原因
Entity类
**/model/entity/*.java
纯数据载体,无逻辑
DTO类
**/model/dto/*.java
纯数据载体,通过其他测试间接覆盖
Req/Resp/Query
**/model/request/*.java
<br>
**/model/response/*.java
<br>
**/model/query/*.java
POJO,通过Controller测试间接覆盖
枚举类
**/enums/*.java
纯定义,无业务逻辑
常量类
**/constants/*.java
仅静态常量
配置类
**/config/**
Spring管理,无业务逻辑
Mapper层
**/mapper/**/*.java
框架生成实现,通过集成测试间接覆盖
MyBatis拦截器
**/*.java
(implements Interceptor)
MyBatis插件框架,通过集成测试覆盖
定时任务
**/job/*.java
通过集成测试覆盖
启动类
*Application.java
无测试价值
事件类
**/*Event.java
纯数据载体,无业务逻辑
事件监听器
**/listener/*Listener.java
Spring框架管理,通过集成测试覆盖
消息队列监听器
**/messagequeue/**
<br>
**/mq/**
MQ框架管理,需要真实环境
TypePackage Path PatternReason
Entity Classes
**/model/entity/*.java
Pure data carrier, no logic
DTO Classes
**/model/dto/*.java
Pure data carrier, indirectly covered by other tests
Req/Resp/Query
**/model/request/*.java
<br>
**/model/response/*.java
<br>
**/model/query/*.java
POJO, indirectly covered by Controller tests
Enum Classes
**/enums/*.java
Pure definition, no business logic
Constant Classes
**/constants/*.java
Only static constants
Configuration Classes
**/config/**
Managed by Spring, no business logic
Mapper Layer
**/mapper/**/*.java
Framework-generated implementation, indirectly covered by integration tests
MyBatis Interceptors
**/*.java
(implements Interceptor)
MyBatis plugin framework, covered by integration tests
Scheduled Jobs
**/job/*.java
Covered by integration tests
Startup Classes
*Application.java
No testing value
Event Classes
**/*Event.java
Pure data carrier, no business logic
Event Listeners
**/listener/*Listener.java
Managed by Spring framework, covered by integration tests
Message Queue Listeners
**/messagequeue/**
<br>
**/mq/**
Managed by MQ framework, requires real environment

JaCoCo排除配置

JaCoCo Exclusion Configuration

pom.xml
中配置排除:
xml
<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.xml
:
xml
<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

标识覆盖率命令
pom.xml
with JaCoCo
mvn test jacoco:report
build.gradle
with JaCoCo
./gradlew test jacocoTestReport
IndicatorCoverage Command
pom.xml
with JaCoCo
mvn test jacoco:report
build.gradle
with JaCoCo
./gradlew test jacocoTestReport

步骤2:分析覆盖率报告

Step 2: Analyze Coverage Report

  1. 运行覆盖率命令
  2. 解析输出(target/site/jacoco/jacoco.xml 或终端输出)
  3. 过滤排除类后,列出低于80%覆盖率的文件,按最差优先排序
  4. 对于每个覆盖率不足的文件,识别:
    • 未测试的方法
    • 缺失的分支覆盖(if/else、switch、异常路径)
    • 死代码(膨胀分母)
  1. Run the coverage command
  2. Parse the output (target/site/jacoco/jacoco.xml or terminal output)
  3. After filtering excluded classes, list files with coverage lower than 80%, sorted by lowest coverage first
  4. 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
PriorityTest TypeCoverage Target
P0Happy PathCore functionality with valid input
P0Exception HandlingInvalid input, business exceptions
P1Edge CasesEmpty collections, null, boundary values
P1Branch CoverageEach 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

  1. 运行完整测试套件 — 所有测试必须通过
  2. 重新运行覆盖率 — 验证改进
  3. 如果仍低于80%,重复步骤3处理剩余缺口
  1. Run the full test suite — all tests must pass
  2. Rerun coverage calculation — verify improvements
  3. 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 TypeTarget CoverageTesting Method
Financial calculation, authentication, security100%Unit Test
Service/Manager business logic80%+Unit Test
Controller Layer80%+Integration Test
Utility classes80%+Unit Test