Spring Boot中JUnit 4与JUnit 5的如何共存

文章目录

  • 前言
  • 一、先上答案
  • 二、稍微深入了解
    • [2.1 maven-surefire-plugin是什么](#2.1 maven-surefire-plugin是什么)
    • [2.2 JUnit4和JUnit5有什么区别](#2.2 JUnit4和JUnit5有什么区别)
      • [2.2.1 不同的注解](#2.2.1 不同的注解)
      • [2.2.2 架构](#2.2.2 架构)

前言

在maven项目中,生成单测时是否有这样的疑问:该选JUnit4还是JUnit5?在执行 mvn test 命令时有没有遇到过有些用例执行不到的情况?如果有,那你是来着了!


一、先上答案

如果你用的maven-surefire-plugin插件是2.19.1版本:

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>mygroup</groupId>
    <artifactId>minimal-conf-junit4-5</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <!-- JUnit 5 depends on JDK 1.8 -->
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <!--  JUnit dependency versions -->
        <junit.version>4.12</junit.version>
        <junit-vintage-engine>4.12.1</junit-vintage-engine>
        <junit-jupiter.version>5.0.1</junit-jupiter.version>
        <junit-platform.version>1.0.1</junit-platform.version>
    </properties>

    <dependencies>
        <!--JUnit Jupiter API to write and compile tests with JUnit5 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- JUnit 4 to make legacy JUnit 4 tests compile -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version> <!-- matters until now-->
                <dependencies>
                    <!-- to let surefire to run JUnit 4 but also JUnit 5 tests -->
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>${junit-platform.version}</version>
                    </dependency>
                    <!-- JUnit vintage engine to run JUnit 3 or JUnit 4 tests -->
                    <dependency>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                        <version>${junit-vintage-engine}</version>
                    </dependency>
                    <!-- JUnit 5 engine to run JUnit 5 tests -->
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>${junit-jupiter.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

如果你用的maven-surefire-plugin插件是2.22.0版本:

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>david</groupId>
    <artifactId>jupiter-4-and-5-same-build</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit-jupiter.version>5.1.0</junit-jupiter.version>
        <!-- optional : if we want to use a junit4 specific version -->
        <junit.version>4.12</junit.version>
    </properties>
    <dependencies>
        <!--JUnit Jupiter Engine to depend on the JUnit5 engine and JUnit 5 API -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <!--JUnit Jupiter Engine to depend on the JUnit4 engine and JUnit 4 API  -->
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>${junit-jupiter.version}</version>
        </dependency>
        <!-- Optional : override the JUnit 4 API version provided by junit-vintage-engine -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
        </plugins>
    </build>

</project>

二、稍微深入了解

2.1 maven-surefire-plugin是什么

Maven是一个项目管理工具,主要用于Java项目的构建和管理。在Maven中,插件是用于扩展其功能的组件。maven-surefire-plugin是其中一个常用的插件,主要用于执行单元测试。

maven-surefire-plugin可以与JUnit和TestNG等单元测试框架集成,通过在构建过程中自动运行测试用例,帮助开发者确保代码的质量和正确性。它可以很好地兼容JUnit 3、JUnit 4以及TestNG,使得开发者可以根据自己的需求选择合适的测试框架。

默认情况下,maven-surefire-plugin会自动执行测试源码路径(默认为src/test/java/)下所有符合一组命名模式的测试类。这组模式包括:

  • /Test.java:任何子目录下所有命名以Test开头的Java类。
  • /Test.java:任何子目录下所有命名以Test结尾的Java类。
  • /TestCase.java:任何子目录下所有命名以TestCase结尾的Java类

知道他是用来跑单测的就行了

2.2 JUnit4和JUnit5有什么区别

2.2.1 不同的注解

大部分注解在JUnit4和JUnit5中都是一样的,但是有些是不一样的,来快速对比一下:

特性 JUnit4 JUnit5
声明一个测试方法 @Test @Test
在当前类的所有测试方法执行前要执行的方法 @BeforeClass @BeforeAll
在当前类的所有测试方法执行后要执行的方法 @AfterClass @AfterAll
每个测试方法执行前要执行的方法 @Before @BeforeEach
每个测试方法执行后要执行的方法 @After @AfterEach
忽略某个测试方法或测试类 @Ignore @Disabled
动态测试用例生成工厂 无此特性 @TestFactory
嵌套测试 无此特性 @Nested
标记与过滤 @Category @Tag
注册定制扩展点 无此特性 @ExtendWith

2.2.2 架构

JUnit4把所有的代码都打包到一个jar包。

JUnit5由三个子项目构成:JUnit平台(JUnit Platform),JUnit Jupiter和JUnit Vintage。

  • JUnit Platform:它定义了测试引擎(TestEngine)API,用于开发运行在JUnit平台上面的新的测试框架。
  • JUnit Jupiter:它拥有所有的新的JUnit注解和测试引擎的实现(Implementation),这个测试引擎的实现能够测试使用新注解开发的测试代码。
  • JUnit Vintage:用于支持在JUnit5平台上运行JUnit3和JUnit4编写的测试用例

参考:https://blog.csdn.net/lzufeng/article/details/127521842

相关推荐
hdsoft_huge7 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
用户21411832636029 小时前
Qwen3-Coder 实战!历史人物短视频一键生成,多分镜人物不崩,魔搭直接玩
后端
追逐时光者9 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 54 期(2025年9.8-9.14)
后端·.net
追逐时光者9 小时前
C#/.NET/.NET Core编程技巧练习集,配套详细的文章教程讲解!
后端·.net
AD钙奶-lalala10 小时前
SpringBoot实现WebSocket服务端
spring boot·后端·websocket
moxiaoran575310 小时前
Flask学习笔记(一)
后端·python·flask
java搬砖工-苤-初心不变10 小时前
基于 lua_shared_dict 的本地内存限流实现
开发语言·junit·lua
你的人类朋友10 小时前
🔒什么是HMAC
后端·安全·程序员
毕设源码-朱学姐10 小时前
【开题答辩全过程】以 4S店汽车维修保养管理系统为例,包含答辩的问题和答案
java·spring boot·汽车
盖世英雄酱5813611 小时前
Read timed out问题 排查
java·数据库·后端