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

如果你用的maven-surefire-plugin插件是2.19.1版本:
<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.0modelVersion>
<groupId>mygroupgroupId>
<artifactId>minimal-conf-junit4-5artifactId>
<version>0.0.1-SNAPSHOTversion>
<properties>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<junit.version>4.12junit.version>
<junit-vintage-engine>4.12.1junit-vintage-engine>
<junit-jupiter.version>5.0.1junit-jupiter.version>
<junit-platform.version>1.0.1junit-platform.version>
properties>
<dependencies>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiter-apiartifactId>
<version>${junit-jupiter.version}version>
<scope>testscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-pluginartifactId>
<version>2.19.1version>
<dependencies>
<dependency>
<groupId>org.junit.platformgroupId>
<artifactId>junit-platform-surefire-providerartifactId>
<version>${junit-platform.version}version>
dependency>
<dependency>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
<version>${junit-vintage-engine}version>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiter-engineartifactId>
<version>${junit-jupiter.version}version>
dependency>
dependencies>
plugin>
plugins>
build>
project>
如果你用的maven-surefire-plugin插件是2.22.0版本:
<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.0modelVersion>
<groupId>davidgroupId>
<artifactId>jupiter-4-and-5-same-buildartifactId>
<version>0.0.1-SNAPSHOTversion>
<properties>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<junit-jupiter.version>5.1.0junit-jupiter.version>
<junit.version>4.12junit.version>
properties>
<dependencies>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiter-engineartifactId>
<version>${junit-jupiter.version}version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
<version>${junit-jupiter.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-pluginartifactId>
<version>2.22.0version>
plugin>
plugins>
build>
project>
Maven是一个项目管理工具,主要用于Java项目的构建和管理。在Maven中,插件是用于扩展其功能的组件。maven-surefire-plugin是其中一个常用的插件,主要用于执行单元测试。
maven-surefire-plugin可以与JUnit和TestNG等单元测试框架集成,通过在构建过程中自动运行测试用例,帮助开发者确保代码的质量和正确性。它可以很好地兼容JUnit 3、JUnit 4以及TestNG,使得开发者可以根据自己的需求选择合适的测试框架。
默认情况下,maven-surefire-plugin会自动执行测试源码路径(默认为src/test/java/)下所有符合一组命名模式的测试类。这组模式包括:
知道他是用来跑单测的就行了
大部分注解在JUnit4和JUnit5中都是一样的,但是有些是不一样的,来快速对比一下:
| 特性 | JUnit4 | JUnit5 |
| 声明一个测试方法 | @Test | @Test |
| 在当前类的所有测试方法执行前要执行的方法 | @BeforeClass | @BeforeAll |
| 在当前类的所有测试方法执行后要执行的方法 | @AfterClass | @AfterAll |
| 每个测试方法执行前要执行的方法 | @Before | @BeforeEach |
| 每个测试方法执行后要执行的方法 | @After | @AfterEach |
| 忽略某个测试方法或测试类 | @Ignore | @Disabled |
| 动态测试用例生成工厂 | 无此特性 | @TestFactory |
| 嵌套测试 | 无此特性 | @Nested |
| 标记与过滤 | @Category | @Tag |
| 注册定制扩展点 | 无此特性 | @ExtendWith |
JUnit4把所有的代码都打包到一个jar包。
JUnit5由三个子项目构成:JUnit平台(JUnit Platform),JUnit Jupiter和JUnit Vintage。

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