全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-23 5 分钟 ✍️ juanwangdev

mvn test 与调试

test 命令执行项目单元测试,生成测试报告。

test 命令

基本用法

Bash
mvn test

执行阶段

Bash
validate → compile → test-compile → test

输出位置

Bash
target/surefire-reports/
├── TEST-com.example.UserServiceTest.xml
├── com.example.UserServiceTest.txt

指定测试类

运行单个测试类

Bash
mvn test -Dtest=UserServiceTest

运行匹配模式的测试

Bash
# 通配符匹配
mvn test -Dtest=*ServiceTest

# 多个测试类
mvn test -Dtest=UserServiceTest,OrderServiceTest

运行特定方法

Bash
mvn test -Dtest=UserServiceTest#testCreate

跳过测试

跳过测试执行

XML
mvn package -DskipTests

跳过测试编译和执行

Bash
mvn package -Dmaven.test.skip=true

区别

参数效果
-DskipTests编译测试代码,不执行
-Dmaven.test.skip=true不编译、不执行

测试配置

surefire 插件配置

Bash
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.22.2</version>
  <configuration>
    <!-- 包含测试类 -->
    <includes>
      <include>**/*Test.java</include>
      <include>**/*Tests.java</include>
    </includes>

    <!-- 排除测试类 -->
    <excludes>
      <exclude>**/*IntegrationTest.java</exclude>
    </excludes>

    <!-- 并行执行 -->
    <parallel>classes</parallel>
    <threadCount>4</threadCount>
  </configuration>
</plugin>

测试失败处理

失败继续构建

Bash
mvn test -Dmaven.test.failure.ignore=true

查看失败详情

XML
# 详细错误日志
mvn test -e

# 查看测试报告
cat target/surefire-reports/com.example.UserServiceTest.txt

测试报告

文本报告

Bash
target/surefire-reports/*.txt

XML 报告

Bash
target/surefire-reports/TEST-*.xml

HTML 报告

使用 surefire-report 插件:

XML
mvn surefire-report:report

输出:target/site/surefire-report.html

测试覆盖率

JaCoCo 配置

XML
<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.8.8</version>
  <executions>
    <execution>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
    </execution>
    <execution>
      <phase>test</phase>
      <goals>
        <goal>report</goal>
      </goals>
    </execution>
  </executions>
</plugin>

生成报告

text
mvn test
mvn jacoco:report

输出:target/site/jacoco/index.html

测试调试

远程调试

text
mvn test -Dmaven.surefire.debug

监听端口 5005,IDE 连接调试。

设置调试端口

text
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <debugForkedProcess>true</debugForkedProcess>
  </configuration>
</plugin>

测试 JVM 参数

text
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <argLine>-Xmx512m -XX:+HeapDumpOnOutOfMemoryError</argLine>
  </configuration>
</plugin>

测试结果概览

输出示例

text
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.example.UserServiceTest
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
Running com.example.OrderServiceTest
Tests run: 3, Failures: 1, Errors: 0, Skipped: 0

Results :
Tests run: 8, Failures: 1, Errors: 0, Skipped: 0

要点总结

  • mvn test 执行单元测试
  • -Dtest=TestClass 指定测试类
  • -Dtest=Class#method 指定方法
  • -DskipTests 跳过测试执行
  • surefire 插件配置测试行为
  • target/surefire-reports 存放测试报告

📝 发现内容有误?点击此处直接编辑

← 上一篇 mvn package 与 install
下一篇 → 常用命令行参数
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库