您可以使用junit-jupiter,这是一个包含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 似乎可以开始(找到 here),链接来自:
对于 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() 并开始一个有用的测试:)