Ali*_*ani 104 java junit maven maven-surefire-plugin junit5
我用JUnit 5编写了一个简单的测试方法:
public class SimlpeTest {
@Test
@DisplayName("Some description")
void methodName() {
// Testing logic for subject under test
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我跑步时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
Run Code Online (Sandbox Code Playgroud)
不知何故,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>
Run Code Online (Sandbox Code Playgroud)
知道如何使这项工作?
Tun*_*aki 107
的maven-surefire-plugin,从今天开始,并没有完全支持JUnit的5.在SUREFIRE-1206中添加此支持有一个未解决的问题.
因此,您需要使用自定义提供程序.其中一个已由JUnit团队开发; 从用户指南中,您需要为新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>
Run Code Online (Sandbox Code Playgroud)
此外,请务必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>
Run Code Online (Sandbox Code Playgroud)
Mik*_*kov 42
问题已在Maven Surefire插件v2.22.0中修复
Maven Central Repository提供新版本.
Maven的
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
摇篮
compile group: 'org.apache.maven.plugins', name: 'maven-surefire-plugin', version: '2.22.0'
Run Code Online (Sandbox Code Playgroud)
正如Marian所指出的,最新版本的JUnit 5 Platform Surefire Provider(1.2.0)支持最新版本的Maven Surefire插件(2.21.0):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</plugin>
Run Code Online (Sandbox Code Playgroud)
例
的pom.xml
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
TestScenario.java
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);
}
}
Run Code Online (Sandbox Code Playgroud)
输出(mvn clean install)
...
[INFO] --- maven-surefire-plugin:2.21.0:test(default-test)@test --- [INFO]
[INFO] -------------- -----------------------------------------
[INFO] TESTS
[INFO] - -------------------------------------------------- ---
[INFO]正在运行test.TestScenario
[INFO]测试运行:1,失败:0,错误:0,跳过:0,经过时间:0.005秒 - 在test.TestScenario
[INFO]
[INFO]结果:
[INFO ]
[INFO] 测试运行:1,失败:0,错误:0,跳过:0
...
迄今为止最简单的方法:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</plugin>
Run Code Online (Sandbox Code Playgroud)
dav*_*xxx 16
从版本开始
2.22.0,Maven Surefire为在JUnit平台上执行测试提供本机支持.
另外,您可以阅读maven-surefire-plugin文档:
使用JUnit 5平台
要开始使用JUnit Platform,您需要在项目中添加至少一个
TestEngine实现.例如,如果要使用Jupiter编写测试,请将测试工件添加junit-jupiter-engine到POM中的依赖项
所以只需要运行JUnit 5测试即可:
<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>
<artifactId>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>
<artifactId>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>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
在我的GitHub空间中,我添加了一个可以浏览/克隆的工作示例maven项目.
网址:https://github.com/ebundy/junit5-minimal-maven-project
wor*_*joe 11
我在 2019 年 8 月遇到了同样的问题,我在这里问过这个问题:Maven 静默找不到 JUnit 测试来运行. 这些答案使我朝着正确的方向前进,但我发现您可以更简洁地解决问题。我从JUnit5 示例 Maven 项目中复制了我的解决方案。
从 JUnit 5.5.1 和maven-surefire-plugin2.22.2 开始,您不需要添加junit-platform-surefire-provider依赖项。在您的pom.xml: 中指定一个依赖项和一个插件就足够了
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
我在JUnit5和Maven中遇到了这个问题,但也注意到,即使仅将 junit-jupiter-engine添加为依赖项,测试仍将在某些项目上运行,而不会在其他项目上运行。我在这里的注释中看到了相同的模式:在上面的@Alex注释中,您可以看到他没有任何问题,即使使用早期版本的surefire / junit / platform。
在摸索了一段时间之后,我意识到那些无法运行测试的项目就是那些测试方法名称不包含单词“ test”的项目。尽管http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html并未强制要求
换句话说:与
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这个
@Test
public void something() {
assertTrue(true);
}
Run Code Online (Sandbox Code Playgroud)
将不会运行,而
@Test
public void testSomething() {
assertTrue(true);
}
Run Code Online (Sandbox Code Playgroud)
将运行!
随着俄罗斯玩偶的发展,这个问题不断发展。
无论如何,@ Mikhail Kholodkov +1,其最新答案立即解决了所有问题!
就我而言,这是因为类路径中的 TestNG ( SUREFIRE-1527 )。Groovy 2.5.5 POM 已经带了它的groovy-testng模块。
手动指定的测试框架提供程序(如https://maven.apache.org/surefire/maven-surefire-plugin/examples/providers.html中所述)解决了问题:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit-platform</artifactId>
<version>2.22.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
作为补充,surefire 2.22.0 + junit 5.2.0 + platform 1.2.0 也可以使用。附件是供您参考的工作 pom:
<?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>
<artifactId>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>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${platform.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit5.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27703 次 |
| 最近记录: |