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 项目中的单元测试。

相关推荐
摇滚侠33 分钟前
VMvare 虚拟机 Oracle19c 安装步骤,远程连接 Oracle19c,百度网盘安装包
java·oracle
梁萌36 分钟前
idea报错找不到XX包的解决方法
java·intellij-idea·启动报错·缺少包
Agent产品评测局43 分钟前
生产排期与MES/ERP系统打通,实操方法详解 —— 2026企业级智能体自动化选型与实战指南
java·运维·人工智能·ai·chatgpt·自动化
阿丰资源1 小时前
基于Spring Boot的电影城管理系统(直接运行)
java·spring boot·后端
呱牛do it1 小时前
企业级门户网站设计与实现:基于SpringBoot + Vue3的全栈解决方案(Day 8)
java
消失的旧时光-19432 小时前
Spring Boot 工程化进阶:统一返回 + 全局异常 + AOP 通用工具包
java·spring boot·后端·aop·自定义注解
NE_STOP2 小时前
Redis--发布订阅命令和Redis事务
java
PAC_3Dame2 小时前
记一次真实的线上OOM
java
SunnyDays10113 小时前
如何在Java中将Word文档转换为图像(JPEG、PNG或SVG)
java
Lumos_7773 小时前
Linux -- 线程
java·jvm·算法