🎊 JUnit5 Maven 依赖项

JUnit5 Maven 依赖项

1. JUnit5 Maven 依赖项JUnit Jupiter 引擎依赖项JUnit 平台运行器依赖项2. 使用 JUnit5 执行 JUnit4 测试3. JUnit5 Maven 示例

原文: https://howtodoinjava.com/junit5/junit5-maven-dependency/

了解使用 Maven 配置其 JUnit5 的不同模块,以及如何使用它们创建和执行测试。

请注意,JUnit5 在运行时需要 Java8。

1. JUnit5 Maven 依赖项要通过 maven 运行 JUnit5 测试,您将至少需要两个依赖项。

JUnit Jupiter 引擎依赖项

JUnit Jupiter 需要具有两个依赖项,即junit-jupiter-api和junit-jupiter-engine。 junit-jupiter-api具有 junit 注解(例如@Test)以编写测试和扩展名,junit-jupiter-engine具有测试引擎实现,在运行时需要执行该引擎才能执行测试。

在内部,junit-jupiter-engine依赖于junit-jupiter-api,因此添加junit-jupiter-engine仅将两个依赖项都带入类路径。

您可以在此图像中了解各种 junit jar 之间的内部依赖项。

org.junit.jupiter junit-jupiter-engine 5.5.2

我们来看看依赖树:

JUnit5 jupiter 引擎依赖树

JUnit 平台运行器依赖项

在 JUnit4 环境中,需要junit-platform-runner用于在 JUnit 平台上执行测试和测试套件。

在内部,junit-platform-runner依赖于junit-platform-suite-api和junit-platform-launcher,因此添加junit-jupiter-engine仅将所有三个依赖项引入类路径中。

org.junit.platform junit-platform-runner ${junit.platform.version} test

我们来看看依赖树:

JUnit5 平台运行器依赖树

2. 使用 JUnit5 执行 JUnit4 测试要在 JUnit5 环境中执行 JUnit4 测试,您将需要JUnit Platform Surefire Provider插件。 只要您在 JUnit4 上配置test依赖项并将 JUnit Vintage TestEngine 实现添加到maven-surefire-plugin的依赖项中,它就可以运行基于 JUnit4 的测试,如下所示。

maven-surefire-plugin 2.19 org.junit.platform junit-platform-surefire-provider 1.0.0-M4 org.junit.vintage junit-vintage-engine 4.12.0-M4 junit junit 4.12 test

通过在pom.xml中进行上述配置,现在您可以使用 JUnit5 运行旧的测试。

3. JUnit5 Maven 示例用于运行用 JUnit5 构建的测试的示例pom.xml文件如下:

4.0.0 com.howtodoinjava JUnit5Examples 0.0.1-SNAPSHOT jar UTF-8 1.8 ${maven.compiler.source} 5.5.2 1.5.2 org.junit.jupiter junit-jupiter-engine ${junit.jupiter.version} test org.junit.platform junit-platform-runner ${junit.platform.version} test maven-compiler-plugin 3.8.1 maven-surefire-plugin 2.22.2

将我的问题放在评论部分。

学习愉快!

源码下载

🎈 相关推荐

梦幻西游手游银币和金币上限是多少 最多拥有多少金币银币
🏷️ Bet体育365提款验证

梦幻西游手游银币和金币上限是多少 最多拥有多少金币银币

📅 09-15 👀 8637
苹果手机充电发热发烫是什么原因?解决方法大公开_手机网易网
Battlefield 2Windows 版
🏷️ Bet体育365提款验证

Battlefield 2Windows 版

📅 08-18 👀 1330
李沁是什么星座?天秤座名人李沁和肖战疑似恋情曝光!
🏷️ office365桌面应用

李沁是什么星座?天秤座名人李沁和肖战疑似恋情曝光!

📅 08-08 👀 8630
微信好友上限多少人?微信好友上限数量满了还能加人吗?
🏷️ Bet体育365提款验证

微信好友上限多少人?微信好友上限数量满了还能加人吗?

📅 07-31 👀 2201
手工编织篮球网教程,篮球网制作方法
🏷️ beat365登录平台

手工编织篮球网教程,篮球网制作方法

📅 07-06 👀 7617