Surefire is not picking up Junit 5 tests
我用JUnit 5编写了一个简单的测试方法:
1 2 3 4 5 6 7 | public class SimlpeTest { @Test @DisplayName("Some description") void methodName() { // Testing logic for subject under test } } |
但是当我运行
1 2 3 4 5 6 7 8 9 | ------------------------------------------------------- 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没有认出那个测试类。 我的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <properties> <java.version>1.8</java.version> <junit.version>5.0.0-SNAPSHOT</junit.version> </properties> <dependencies> <dependency> <groupId>org.junit</groupId> 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> maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> </plugins> </build> |
知道如何使这项工作?
截至今天,
因此,您需要使用自定义提供程序。其中一个已由JUnit团队开发;从用户指南中,您需要为新API添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <build> <plugins> <plugin> 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> junit-platform-surefire-provider</artifactId> <version>1.0.3</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-engine</artifactId> <version>5.0.3</version> </dependency> </dependencies> </plugin> </plugins> </build> |
另外,请确保使用
1 2 3 4 5 6 7 8 | <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-api</artifactId> <version>5.0.3</version> <scope>test</scope> </dependency> </dependencies> |
更新2
问题已在Maven Surefire插件v2.22.0中修复
Maven Central Repository提供新版本。
Maven的
1 2 3 4 5 | <dependency> <groupId>org.apache.maven.plugins</groupId> maven-surefire-plugin</artifactId> <version>2.22.0</version> </dependency> |
摇篮
1 | compile group: 'org.apache.maven.plugins', name: 'maven-surefire-plugin', version: '2.22.0' |
更新
正如Marian所指出的,最新版本的JUnit 5 Platform Surefire Provider(1.2.0)支持最新版本的Maven Surefire插件(2.21.0):
1 2 3 4 5 6 7 8 9 10 11 12 | <plugin> <groupId>org.apache.maven.plugins</groupId> maven-surefire-plugin</artifactId> <version>2.21.0</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> junit-platform-surefire-provider</artifactId> <version>1.2.0</version> </dependency> </dependencies> </plugin> |
例
的pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-engine</artifactId> <version>5.2.0</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> maven-surefire-plugin</artifactId> <version>2.21.0</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> junit-platform-surefire-provider</artifactId> <version>1.2.0</version> </dependency> </dependencies> </plugin> </plugins> </build> |
TestScenario.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package test; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; public class TestScenario { @Test @DisplayName("Test 2 + 2 = 4") public void test() { Assertions.assertEquals(4, 2 + 2); } } |
输出(mvn clean install)
...
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ test ---
[INFO] [INFO]
------------------------------------------------------- [INFO] T E S T S [INFO]
------------------------------------------------------- [INFO] Running test.TestScenario [INFO] Tests run: 1, Failures: 0,
Errors: 0, Skipped: 0, Time elapsed: 0.005 s - in test.TestScenario
[INFO] [INFO] Results: [INFO] [INFO] Tests run: 1,
Failures: 0, Errors: 0, Skipped: 0
...
迄今为止最简单的方法:
1 2 3 4 5 6 7 8 9 10 11 | <plugin> maven-surefire-plugin</artifactId> <version>2.19.1</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> junit-platform-surefire-provider</artifactId> <version>1.1.0</version> </dependency> </dependencies> </plugin> |
从JUnit 5文档:
Starting with version
2.22.0 , Maven Surefire provides native support
for executing tests on the JUnit Platform.
另外,您可以阅读
Using JUnit 5 Platform
To get started with JUnit Platform, you need to add at least a single
TestEngine implementation to your project. For example, if you want to
write tests with Jupiter, add the test artifactjunit-jupiter-engine
to the dependencies in POM
所以只需要运行JUnit 5测试即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <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>davidxxx</groupId> minimal-pom-junit5</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <junit-jupiter.version>5.2.0</junit-jupiter.version> <!--optional below but good practice to specify our java version--> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-engine</artifactId> <version>${junit-jupiter.version}</version> <scope>test</scope> </dependency> <!--optional below --> <!-- add any JUnit extension you need such as --> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-params</artifactId> <version>${junit-jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> maven-surefire-plugin</artifactId> <version>2.22.0</version> </plugin> </plugins> </build> </project> |
在我的GitHub空间中,我添加了一个可以浏览/克隆的工作示例maven项目。
网址:https://github.com/ebundy/junit5-minimal-maven-project
我使用JUnit5和Maven遇到了这个问题但是也注意到,即使只添加了junit-jupiter-engine作为依赖项,测试也会在某些项目上运行,而不是在其他项目上运行。我在这里的评论中看到了相同的模式:在上面的@Alex评论中你可以看到他没有任何问题,即使是早期版本的surefire / junit / platform。
在我挠了一段时间后,我意识到那些测试不会运行的项目是那些测试方法名称不包含单词"test"的项目。虽然这不是http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html强制要求的。
换一种说法:
只是
1 2 3 4 5 6 | <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-engine</artifactId> <version>5.2.0</version> <scope>test</scope> </dependency> |
这个
1 2 3 4 | @Test public void something() { assertTrue(true); } |
不会被运行,而
1 2 3 4 | @Test public void testSomething() { assertTrue(true); } |
将被运行!
这个问题作为一个俄罗斯娃娃展开......
无论如何,为@Mikhail Kholodkov +1,其更新的答案立刻解决了所有问题!
为了补充,surefire 2.22.0 + junit 5.2.0 +平台1.2.0也可以。附件是您的参考的工作pom:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | <?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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.jjhome.junit5</groupId> junit5-hone</artifactId> <packaging>jar</packaging> <version>1.0.0-SNAPSHOT</version> <name>junit5-home</name> <properties> <maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit5.version>5.2.0</junit5.version> <platform.version>1.2.0</platform.version> <surefire.version>2.22.0</surefire.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-api</artifactId> <version>${junit5.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-params</artifactId> <version>${junit5.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-engine</artifactId> <version>${junit5.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.vintage</groupId> junit-vintage-engine</artifactId> <version>${junit5.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> junit-platform-launcher</artifactId> <version>${platform.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> junit-platform-runner</artifactId> <version>${platform.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> maven-surefire-plugin</artifactId> <version>${surefire.version}</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> junit-platform-surefire-provider</artifactId> <version>${platform.version}</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-engine</artifactId> <version>${junit5.version}</version> </dependency> </dependencies> </plugin> </plugins> </build> </project> |
在我的情况下,这是因为类路径中的TestNG(SUREFIRE-1527)。 Groovy 2.5.5 POM带来了
手动指定的测试框架提供程序(如https://maven.apache.org/surefire/maven-surefire-plugin/examples/providers.html中所述)解决了这个问题:
1 2 3 4 5 6 7 8 9 10 | <plugin> <groupId>org.apache.maven.plugins</groupId> maven-surefire-plugin</artifactId> <version>2.22.1</version> <dependency> <groupId>org.apache.maven.surefire</groupId> surefire-junit-platform</artifactId> <version>2.22.1</version> </dependency> |
我有类似的问题也导致Surefire识别零测试。
我的问题结果与以下内容有关(来自JUnit 5.1.0 / maven文档):
Due to a memory leak in Surefire 2.20 and issues running on Java 9, the junit-platform-surefire-provider currently only works with Surefire 2.19.1.
我试图使用最新版本的Surefire(2.21.0)和junit-platform-surefire-provider(1.1.0),它不起作用(既不是Java 8也不是9)
切换回Surefire 2.19.1解决了我的问题。
根据这个问题,一个修复程序将包含在junit-platform-surefire-provider的1.2.0版本中(目前仅作为SNAPSHOT提供)。
肯定会有2.20开放的问题
它适用于surfire 2.19 + junit-platform- * 1.0.3
有一点我注意到我能够让它工作:
-
命名我的测试类
ClinicCalendarShould 不会被maven拿起 -
命名我的测试类
ClinicCalendarTest 由maven接收
因此,除非我在surefire插件中缺少某种配置或参数或其他内容,否则默认情况下,您需要将测试类命名为XXXTest。
更新到
但我在Junit5上使用