我用JUnit 5编写了一个简单的测试方法:
public class SimlpeTest {
@Test
@DisplayName("Some description")
void methodName() {
// Testing logic for subject under test
}
}
但是当我运行
mvn test
时,我得到了:-------------------------------------------------------
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
看起来像:<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>
任何想法如何使这项工作?
最佳答案
到今天为止,maven-surefire-plugin
执行not have full support of JUnit 5。在SUREFIRE-1206中添加此支持存在一个 Unresolved 问题。
因此,您需要使用custom provider。 JUnit团队已经开发了一种。从user guide中,您需要为新的API添加junit-platform-surefire-provider
提供程序和TestEngine
实现:
<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
:<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.3</version>
<scope>test</scope>
</dependency>
</dependencies>