Maven 构建 & 项目测试(完整实战 · 2025 最新版)
目标 :掌握 Maven 构建流程 + 单元测试 + 集成测试 + 测试报告 + 跳过策略
一、Maven 构建全流程回顾
validate → compile → test → package → verify → install → deploy
阶段 命令 输出 compilemvn compiletarget/classestestmvn testtarget/test-classes + 报告packagemvn packagetarget/*.jarinstallmvn install本地仓库 ~/.m2 deploymvn deploy远程仓库
二、测试类型与插件
测试类型 插件 目录 命名规范 单元测试 maven-surefire-pluginsrc/test/java*Test.java集成测试 maven-failsafe-pluginsrc/test/java*IT.java
三、项目结构(标准)
my-test-app/
├── pom.xml
└── src/
├── main/
│ └── java/com/example/Calculator.java
└── test/
├── java/
│ ├── com/example/CalculatorTest.java ← 单元测试
│ └── com/example/CalculatorIT.java ← 集成测试
└── resources/
└── test-data.json
四、核心 pom.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-test-app</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.10.3</junit.version>
</properties>
<dependencies>
<!-- JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Mockito(模拟) -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 单元测试:surefire -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
<excludes>
<exclude>**/*IT.java</exclude>
</excludes>
<!-- 失败时继续 -->
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<!-- 集成测试:failsafe -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.2.5</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<!-- 打包可执行 JAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>shade</goal></goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.Calculator</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<!-- 测试报告 -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<goals><goal>prepare-agent</goal></goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals><goal>report</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
五、代码示例
src/main/java/com/example/Calculator.java
package com.example;
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int divide(int a, int b) {
if (b == 0) throw new ArithmeticException("除零");
return a / b;
}
}
单元测试:CalculatorTest.java
package com.example;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import org.mockito.*;
class CalculatorTest {
private Calculator calc;
@BeforeEach
void setUp() {
calc = new Calculator();
}
@Test
void testAdd() {
assertEquals(5, calc.add(2, 3));
}
@Test
void testDivideByZero() {
assertThrows(ArithmeticException.class, () -> calc.divide(1, 0));
}
@Test
@Disabled("临时跳过")
void skippedTest() {
fail("此测试被禁用");
}
}
集成测试:CalculatorIT.java
package com.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class CalculatorIT {
@Test
void integrationTest() throws Exception {
Calculator calc = new Calculator();
assertEquals(10, calc.add(4, 6));
// 模拟数据库/网络等
Thread.sleep(100);
}
}
六、执行测试命令
命令 作用 mvn test只运行单元测试 (surefire)mvn verify运行单元 + 集成测试 (surefire + failsafe)mvn test -Dtest=CalculatorTest运行指定测试类 mvn test -Dtest=*IT运行所有集成测试 mvn test -DfailIfNoTests=false无测试不失败
七、跳过测试(4 种方式)
方式 命令/配置 命令行 mvn package -DskipTests跳过编译+执行 mvn package -Dmaven.test.skip=truepom.xml 配置 <skipTests>true</skipTests>Profile 控制 -Pprod 跳过测试
<!-- Profile 示例 -->
<profile>
<id>prod</id>
<properties>
<skipTests>true</skipTests>
</properties>
</profile>
八、生成测试报告
1. Surefire 报告(HTML)
mvn test
# → target/surefire-reports/
2. JaCoCo 代码覆盖率
mvn test
# → target/site/jacoco/index.html
打开浏览器查看覆盖率!
九、构建 + 测试完整流程
# 1. 清理 + 编译 + 单元测试 + 打包
mvn clean package
# 2. 运行集成测试 + 验证
mvn verify
# 3. 生成报告
open target/site/jacoco/index.html
十、IDEA 集成测试
右键测试类 → Run ‘CalculatorTest’
Maven 面板 → verify 一键运行
Coverage 工具查看覆盖率
十一、常见问题
问题 解决方案 No tests found检查命名 *Test.java IT.java 不执行必须用 mvn verify 测试卡住mvn test -DforkCount=0内存溢出export MAVEN_OPTS="-Xmx2g"
恭喜!你已掌握 Maven 构建 + 测试!
下一步推荐
方向 内容 Spring Boot 测试 @SpringBootTestMockito 高级 @Mock, @InjectMocksCI/CD 测试 GitHub Actions + mvn verify 性能测试 JMeter + Maven
需要 Spring Boot 测试完整示例 ? 回复 Spring Boot Test 立即获取!