如何使 Maven 与 JUnit 兼容?
How to make Maven play nice with JUnit?
Maven surefire 插件仅运行以单词 test
开头或结尾的测试。这在 JUnit 中处理起来真的很麻烦,原因有两个:
1 - 我已经必须将 @Test
添加到我的所有测试方法中,因此还必须附加单词 test
是重复的。
2 - 在 JUnit 中,如果我想禁用某些测试,我只是用 @Disabled
标记它,所以如果我使用 surefire,我还必须重命名测试方法。
有什么方法可以让 surefire 与 JUnit 兼容?这样,只有 运行 标有 @Test
并自动忽略 @Disabled
方法的内容?
目前我的pom.xml
是这样的(只包括要保存的测试相关项目space):
<packaging>jar</packaging>
<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>
</properties>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<build>
<pluginManagement>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</pluginManagement>
</build>
为了 运行 使用 Maven 进行 JUnit Jupiter 测试,您必须使用 junit-jupiter-engine instead of junit-jupiter-api。
<dependencies>
[...]
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
[...]
</dependencies>
您可以添加 junit-jupiter-api 作为补充工件。此外,您至少应该使用 maven-surefire-plugin 2.22.1+
Maven surefire 插件仅运行以单词 test
开头或结尾的测试。这在 JUnit 中处理起来真的很麻烦,原因有两个:
1 - 我已经必须将 @Test
添加到我的所有测试方法中,因此还必须附加单词 test
是重复的。
2 - 在 JUnit 中,如果我想禁用某些测试,我只是用 @Disabled
标记它,所以如果我使用 surefire,我还必须重命名测试方法。
有什么方法可以让 surefire 与 JUnit 兼容?这样,只有 运行 标有 @Test
并自动忽略 @Disabled
方法的内容?
目前我的pom.xml
是这样的(只包括要保存的测试相关项目space):
<packaging>jar</packaging>
<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>
</properties>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<build>
<pluginManagement>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</pluginManagement>
</build>
为了 运行 使用 Maven 进行 JUnit Jupiter 测试,您必须使用 junit-jupiter-engine instead of junit-jupiter-api。
<dependencies>
[...]
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
[...]
</dependencies>
您可以添加 junit-jupiter-api 作为补充工件。此外,您至少应该使用 maven-surefire-plugin 2.22.1+