【发布时间】:2020-07-01 17:23:50
【问题描述】:
我正在尝试配置 Java Maven 项目以在构建时运行 JUnit 5 测试。
我有一个 Java 项目的标准文件结构,我的项目包含一个 src/main/java 文件夹,其中包含几个构建良好的文件,我最近为 JUnit 测试添加了一个 src/test/java 文件夹。
在src/test/java 中,我有一个包含 JUnit 测试的文件,如下所示:
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class SampleTests {
@Test
@DisplayName("This Test Should fail")
public void testThatAlwaysFails() {
assertEquals(3, 2);
}
}
以下是我的pom.xml 文件的相关部分:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.jupiter.version>5.6.2</junit.jupiter.version>
</properties>
<dependencies>
...
<!-- For JUnit 5-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- For Running the JUnits-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
</project>
这一切似乎都符合我找到的文档以及其他 SO 帖子中的解决方案。但是,当我通过mvn clean install 构建项目时,这些是与测试相关的唯一输出行:
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (groovy-tests) @ <project-name-here> ---
[INFO] Changes detected - recompiling the module!
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ <project-name-here> ---
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ <project-name-here> ---
(我已经从输出中删除了实际的项目名称,但正如预期的那样)。
这是什么原因,我如何配置我的pom 文件以便运行测试?我希望在maven-surefire-plugin 行之后有一行输出选择提供程序,然后它会说它找到了哪些测试和结果,但是这些都没有出现在控制台中。我已经为 JUnit Jupiter 和 Surefire 插件尝试了许多版本,同样的事情也发生了。我相信我曾经使用 JUnit 5 和 Surefire 插件的较旧设置让它工作,但我不记得版本,如果可能的话,我希望它使用这些依赖项的最新版本工作。
如果有什么不同,我也会从 IntelliJ IDEA Maven 配置中运行 mvn clean install。
【问题讨论】:
-
Maven Surefire 插件页面中有一个专门的部分描述了如何使用 Maven Surfire 设置 JUnit 5:maven.apache.org/surefire/maven-surefire-plugin/examples/…
标签: java maven junit5 maven-surefire-plugin