maven - 关于设置 Maven 依赖项的 JUnit5 文档的说明
问题描述
JUnit5 页面要求在 StackOverflow 上发布澄清问题,所以在这里。
用户指南说 JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage,并且 JUnit Platform 是“启动测试框架的基础” 。所以 JUnit Platform 听起来很重要。
它还说“要了解哪些工件可供下载并包含在您的项目中,请参阅依赖元数据。” 当你去那里时,有关 JUnit 平台的信息,10.1.1。JUnit Platform, Group ID: org.junit.platform, Version: 1.6.2 列在首位。所以听起来你必须将它包含在你的依赖项中。
但是,用户指南也建议去Build Support。在那里,Maven 指令建议包含maven-surefire-plugin
、maven-failsafe-plugin
和。junit-jupiter-api
junit-jupiter-engine
用户指南还建议查看示例项目。在那里,Maven 示例仅包含 artifact junit-jupiter
。
所以我的问题是:遵循哪个建议?实际的指令是什么?
对于普通的最终用户,似乎最好的选择是遵循示例项目。但是,它显示为用户指南页面中的最后一个建议,并且似乎与之前的解释相矛盾。这会产生一些不必要的猜测,我建议简化用户指南的这一部分,以便更直接地切入主题。
解决方案
您可以使用junit-jupiter
which 是一个同时包含junit-jupiter-api
和的模块junit-jupiter-engine
。
/**
* Aggregates all JUnit Jupiter modules.
*
* @since 5.4
*/
module org.junit.jupiter {
requires transitive org.junit.jupiter.api;
requires transitive org.junit.jupiter.engine;
requires transitive org.junit.jupiter.params;
}
这个 pom 似乎可以开始(在此处找到),链接自:
对于 Maven,请查看 junit5-jupiter-starter-maven 项目。
<?xml version="1.0" encoding="UTF-8"?>
<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>junit5-jupiter-starter-maven</artifactId>
<version>1.0-SNAPSHOT</version>
<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.6.2</junit.jupiter.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.jupiter.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>
所以:
- 如上创建 pom.xml
- 在 src/test/java/org/example/MyExampleTest.java 中创建测试类:
package org.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class MyExampleTest {
@Test
void thisFailsTest() {
fail(); //start here
}
}
- 从终端执行
mvn test
- 构建将像这样失败
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ junit5-jupiter-starter-maven ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running org.example.MyExampleTest
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.017 s <<< FAILURE! - in org.example.MyExampleTest
[ERROR] thisFailsTest Time elapsed: 0.014 s <<< FAILURE!
org.opentest4j.AssertionFailedError:
at org.example.MyExampleTest.thisFailsTest(MyExampleTest.java:10)
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] MyExampleTest.thisFailsTest:10
[INFO]
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
- 删除 fail() 并开始一个有用的测试:)
推荐阅读
- javascript - 如何在加载 dom 树后立即运行 javascript 脚本以避免用户看到“单缓冲”效果?
- shake-build-system - 摇一摇:命令失败后自动删除文件
- python - 有没有更好的方法来使用另一个单元格在 Numpy 矩阵中创建一个值
- xml - 解析动态 XML
- maven - .m2 文件夹通过 IntelliJ 是不可见的
- asp.net-core - _context.SaveChanges() 有效,但 await _context.SaveChangesAsync() 无效
- ios - 如何使用json解析选择所有表视图行并获取所有行的id?
- spring - 记录大消息的最佳实践
- python - Django 后台任务与 Celery
- python - 从嵌套字典中提取数据,TypeError:字符串索引必须是整数