多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# JUnit5 Maven 依赖项 > 原文: [https://howtodoinjava.com/junit5/junit5-maven-dependency/](https://howtodoinjava.com/junit5/junit5-maven-dependency/) 了解**使用 Maven** 配置其 JUnit5 的不同模块,以及如何使用它们创建和执行测试。 请注意,JUnit5 在运行时需要 Java8。 ## 1\. JUnit5 Maven 依赖项 要通过 [maven](https://howtodoinjava.com/maven/) 运行 [JUnit5](https://howtodoinjava.com/junit-5-tutorial/) 测试,您将至少需要两个依赖项。 1. #### 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`仅将两个依赖项都带入类路径。 您可以在此[图像](http://junit.org/junit5/docs/current/user-guide/images/component-diagram.svg)中了解各种 junit jar 之间的**内部依赖项**。 ```java <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.5.2</version> </dependency> ``` 我们来看看依赖树: ![JUnit5 jupiter engine dependency tree](https://img.kancloud.cn/a5/45/a5459b81f1aeccac3c68b86cb730e0ee_499x297.png) JUnit5 jupiter 引擎依赖树 2. #### JUnit 平台运行器依赖项 在 JUnit4 环境中,需要`junit-platform-runner`用于**在 JUnit 平台上执行测试和测试套件**。 在内部,`junit-platform-runner`依赖于`junit-platform-suite-api`和`junit-platform-launcher`,因此添加`junit-jupiter-engine`仅将所有三个依赖项引入类路径中。 ```java <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-runner</artifactId> <version>${junit.platform.version}</version> <scope>test</scope> </dependency> ``` 我们来看看依赖树: ![JUnit5 platform runner dependency tree](https://img.kancloud.cn/58/78/5878ff48dbb94584e795beb1116cb7e8_486x289.png) JUnit5 平台运行器依赖树 ## 2\. 使用 JUnit5 执行 JUnit4 测试 要在 JUnit5 环境中执行 **JUnit4 测试**,您将需要`JUnit Platform Surefire Provider`插件。 只要您在 JUnit4 上配置`test`依赖项并将 JUnit Vintage TestEngine 实现添加到`maven-surefire-plugin`的依赖项中,它就可以运行基于 JUnit4 的测试,如下所示。 ```java <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>1.0.0-M4</version> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>4.12.0-M4</version> </dependency> </dependencies> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> ``` 通过在`pom.xml`中进行上述配置,现在您可以使用 JUnit5 运行旧的测试。 ## 3\. JUnit5 Maven 示例 用于运行用 JUnit5 构建的测试的示例`pom.xml`文件如下: ```java <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.howtodoinjava</groupId> <artifactId>JUnit5Examples</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>${maven.compiler.source}</maven.compiler.target> <junit.jupiter.version>5.5.2</junit.jupiter.version> <junit.platform.version>1.5.2</junit.platform.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-runner</artifactId> <version>${junit.platform.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> </plugin> </plugins> </build> </project> ``` 将我的问题放在评论部分。 学习愉快! [源码下载](https://github.com/lokeshgupta1981/Junit5Examples/tree/master/JUnit5Examples)