Maven Surefire 插件简介

Maven Surefire 插件是 Maven 构建系统中的一个关键组件,专门用于在构建生命周期中执行单元测试。

它通常与 Maven 构建生命周期的测试阶段绑定,确保所有单元测试在项目编译后和打包前被执行。

最新版本

Maven Surefire 插件的最新版本为 3.5.2。

使用最新版本可以确保访问到最新的特性和改进。

配置 Maven Surefire 插件

要使用 Maven Surefire 插件,您需要在项目的 pom.xml 文件中进行配置。下面将通过步骤介绍如何在一个 Maven 项目中设置 Surefire 插件。

步骤 1: 创建 Maven 项目

运行以下命令创建一个新的 Maven 项目:

bash 复制代码
mvn archetype:generate -DgroupId=com.example -DartifactId=surefire-plugin-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

该命令会生成一个简单的 Maven 项目,其目录结构如下:

复制代码
surefire-plugin-demo
|-- src
|   |-- main
|   |   `-- java
|   |       `-- com
|   |           `-- example
|   |               `-- App.java
|   `-- test
|       `-- java
|           `-- com
|               `-- example
|                   `-- AppTest.java
|-- pom.xml
`-- target
步骤 2: 添加 Maven Surefire 插件配置

进入项目目录并打开 pom.xml 文件,在 <build> 标签内添加 Maven Surefire 插件的配置:

xml 复制代码
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.example</groupId>
    <artifactId>surefire-plugin-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M8</version>
            </plugin>
        </plugins>
    </build>
</project>
  • <groupId>: 指定 Maven Surefire 插件的组ID。
  • <artifactId>: 指定 Maven Surefire 插件。
  • <version>: 使用的插件版本。请确保使用最新版本。
步骤 3: 创建单元测试

在 src/test/java 目录下创建一个单元测试文件,例如 AppTest.java:

java 复制代码
package com.example;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AppTest {

    @Test
    public void sampleTest() {
        System.out.println("正在运行单元测试...");
        assertTrue(true);
    }
}
步骤 4: 构建并运行单元测试

使用以下命令构建项目并运行单元测试:

bash 复制代码
mvn clean test

构建完成后,单元测试将在测试阶段运行。

步骤 5: 查看结果

输出将包括单元测试的结果:

复制代码
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
运行 com.example.AppTest
正在运行单元测试...
测试运行数: 1, 失败数: 0, 错误数: 0, 跳过数: 0, 总耗时: 0.001 秒
结果:

测试运行数: 1, 失败数: 0, 错误数: 0, 跳过数: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

高级配置

Maven Surefire 插件提供了多种配置选项来定制测试行为。以下是一些常用的配置示例:

指定测试包含/排除规则

可以在 <plugin> 标签内添加以下配置来指定要包含或排除的测试:

xml 复制代码
<configuration>
    <includes>
        <include>**/*Test.java</include>
        <include>**/*Tests.java</include>
        <include>**/*TestCase.java</include>
    </includes>
    <excludes>
        <exclude>**/*IntegrationTest.java</exclude>
    </excludes>
</configuration>
设置系统属性

可以设置系统属性,使这些属性在测试中可用:

xml 复制代码
<configuration>
    <systemPropertyVariables>
        <propertyName>propertyValue</propertyName>
    </systemPropertyVariables>
</configuration>
并行运行测试

可以通过以下配置并行运行测试以加速测试过程:

xml 复制代码
<configuration>
    <parallel>methods</parallel>
    <threadCount>4</threadCount>
</configuration>

总结

Maven Surefire 插件是 Maven 项目中运行单元测试的重要工具。

通过使用此插件,可以确保在构建过程中运行单元测试,有助于保持代码质量并及早发现问题。

本指南全面介绍了设置和使用 Maven Surefire 插件的方法,并提供了一个实际示例帮助您入门。

掌握这些知识后,您可以有效地管理和运行 Maven 项目中的单元测试。

相关推荐
考虑考虑4 小时前
JDK27结构化并发
java·后端·java ee
Cosolar7 小时前
监控指标全正常,订单接口从 80ms 涨到 4s 故障复盘
java·后端·面试
会编程的吕洞宾15 小时前
Spring AI 2.0 工具调用实战:让 AI 自动调用你的 Java 方法
java·人工智能·经验分享·后端·spring
斯维赤15 小时前
SELECT * 为什么是覆盖索引的天敌?
java·后端·mysql
stormsha16 小时前
库存 5,10 个并发各买 1 件:为什么最后不剩 -5 件
java·数据库·mysql
RobinXYuan16 小时前
【Java 字节码探秘】02 ClassFile 骨架:十个字段
java·jvm·源码阅读
炸薯条!16 小时前
Python面向对象基础:类与对象、实例方法、魔法方法和属性详解
java·开发语言·python
SimonKing17 小时前
又一款数据库同步神器,2.3K Star的dbswitch
java·后端·程序员
CoderYanger18 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第二弹):DispatcherServlet 源码分析+适配器模式
java·数据库·hive·spring boot·面试·java-ee·mybatis
Rain的Java大神之路18 小时前
前端转 Java 全栈:用 AI 两天交付一个能跑的 Demo,踩坑全记录
java·前端·后端