Surefire没有接受Junit 5测试 [英] Surefire is not picking up Junit 5 tests
问题描述
我用JUnit 5写了一个简单的测试方法:
I wrote a simple test method with JUnit 5:
public class SimlpeTest {
@Test
@DisplayName("Some description")
void methodName() {
// Testing logic for subject under test
}
}
但是当我运行 mvn test
时,我得到了:
But when I run mvn test
, I got:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running SimlpeTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
不知何故,surefire无法识别该测试类。我的 pom.xml
如下所示:
Somehow, surefire didn't recognize that test class. My pom.xml
looks like:
<properties>
<java.version>1.8</java.version>
<junit.version>5.0.0-SNAPSHOT</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<updatePolicy>always</updatePolicy>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
任何想法如何使这项工作?
Any idea how to make this work?
推荐答案
截至今天, maven-surefire-plugin
执行没有完全支持JUnit 5 。有关在 SUREFIRE-1206 中添加此支持的未解决问题。
The maven-surefire-plugin
, as of today, does not have full support of JUnit 5. There is an open issue about adding this support in SUREFIRE-1206.
因此,您需要使用自定义提供商。其中一个已由JUnit团队开发;从用户指南中,您需要为新API添加 junit-platform-surefire-provider
提供程序和 TestEngine
实现:
As such, you need to use a custom provider. One has already been developed by the JUnit team; from the user guide, you need to add the junit-platform-surefire-provider
provider and the TestEngine
implementation for the new API:
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<!-- latest version (2.20.1) does not work well with JUnit5 -->
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
此外,请务必声明 junit-jupiter-api
依赖范围 test
:
Also, be sure to declare the junit-jupiter-api
dependency with a scope of test
:
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.3</version>
<scope>test</scope>
</dependency>
</dependencies>
这篇关于Surefire没有接受Junit 5测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!