【发布时间】:2019-06-16 15:29:28
【问题描述】:
任何人都可以在几分钟内轻松重现此问题。
基础 Maven quickstart 项目
借助 IntelliJ 2018.3 和 Maven 3.6.0,我使用 Maven 原型 maven-archetype-quickstart 1.4 版创建了一个全新的项目。
Java 11
在新项目的 POM 文件中,我将 maven.compiler.source 和 maven.compiler.target 的属性从 1.7 更改为 11,对于我当前使用的 Java 11.0.2,Zulu 从 Azul Systems。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
在 IntelliJ 的 Maven 面板上,我运行 clean 和 install 生命周期事件。
在 JUnit 4 中运行测试
作为install 的一部分,运行测试。这个quickstart 原型带有一个断言true 的单一测试。
结果显示在 IntelliJ 的 Run 面板中。
[INFO] 运行 work.basil.example.AppTest
[INFO] 测试运行:1,失败:0,错误:0,跳过:0,经过时间:0.026 秒 - 在 work.basil.example.AppTest 中
所以我知道执行的测试。
JUnit 5,而不是 4
这一切都很好。现在让我们升级到 JUnit 5,看看问题所在。
在 POM 中,我从这里更改了 JUnit 依赖项:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
……到这个:
<dependencies>
<!--JUnit 5-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
</dependencies>
木星导入(无老式测试)
编译器抱怨我的AppTest.java 文件。因此,我将那里的 import 语句更改为使用 jupiter 包。我只想在我的新 greedfield 项目中运行 JUnit 5 测试,而不需要老式的 JUnit 4 测试。所以进口从这个改变:
import static org.junit.Assert.assertTrue;
import org.junit.Test;
……到这个:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
然后我执行Maven > Lifecycle > clean & install。
...瞧,问题是:我们的测试没有执行。在 IntelliJ 的Run 面板中看到的报告:
[INFO] 运行 work.basil.example.AppTest
[INFO] 测试运行:0,失败:0,错误:0,跳过:0,经过时间:0.003 秒 - 在 work.basil.example.AppTest 中
➥ 为什么 JUnit 5 无法运行 JUnit 4 愉快运行的测试?
更新surefire插件
我怀疑Maven Surefire Plugin 需要更新。所以在 POM 中我改变了这个:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
……到这个:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
另一个clean & install。但没有更好,仍然运行 0 次测试。
[INFO] 运行 work.basil.example.AppTest
[INFO] 测试运行:0,失败:0,错误:0,跳过:0,经过时间:0.004 秒 - 在 work.basil.example.AppTest 中
整个 POM
这是我的整个 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>work.basil.example</groupId>
<artifactId>tester</artifactId>
<version>1.0-SNAPSHOT</version>
<name>tester</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!--JUnit 5-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
JUnit 库
在做了一个 Maven clean & install 之后,出现了两个 JUnit 库:junit-jupiter-api 和 junit-platform-commons。
其他版本的 JUnit 5
我在 junit-jupiter-api 依赖项中尝试了以下版本:
- 5.0.0-M1
- 5.1.1
- 5.3.0
- 5.3.2
- 5.4.0-M1
每次尝试时,我都会运行一个 Maven clean 和 install。没有更好的。这些版本中的每一个都报告了Tests run: 0。
不怪maven-archetype-quickstart
我实际上是在使用完全不同的 Maven 原型的一个完全不同的项目中发现了这个问题。
为了确定这个错误的 JUnit 5 行为,我使用非常简单的 maven-archetype-quickstart 尝试了一个新的新项目。我发现了同样的行为:一切都编译了,测试工具正在运行,但在 JUnit 5 下没有执行任何测试。
【问题讨论】:
-
你知道 junit-vintage-engine maven artefact 吗?根据我的经验,要么添加它以运行未修改的旧测试,要么投入一些工作来遵循他们的迁移文档 (junit.org/junit5/docs/current/user-guide/#migrating-from-junit4)。
-
@JensDibbern 我没有运行老式测试。在我的问题中,我解释说我将
import语句更改为jupiter。在我的新新建项目中,我只需要 JUnit 5。 -
您是否尝试过其他版本,例如。 5.3.0,或者可能是 5.0.0-M1,看看是否有效?由于不是来自同一个地方,可能仍然存在与 4 个版本测试的兼容性问题,但在简单的测试中它应该可以工作。
-
@TraianGEICU 版本 5.0.0-M1、5.1.1、5.3.0、5.3.2 和 5.4.0-M1 的
junit-jupiter-api依赖项都无法运行测试。在问题中添加了部分。 -
@TraianGEICU 谢谢!这似乎解决了我的问题,添加了这两个依赖项中的一个:
junit-jupiter-engine。我发布了an Answer 的详细信息。我遵循了其他地方的指示,说只需要一个junit-jupiter-api依赖项,但显然这些指示是错误的。
标签: java intellij-idea junit junit5 junit-jupiter