确认 IDEA 版本

首先我的 IDEA 版本是 2019.2,是反对应用 Junit5 的,所以应该不须要装置额定的插件。

Idea 官网博客示意反对JUnit5测试框架是IntelliJ IDEA 2016.2新个性的其中一个:

Using JUnit 5 in IntelliJ IDEA

国内的媒体也翻译并转发了这篇报道:

搜狐:在IntelliJ IDEA中应用JUnit5

那问题就在于我应用 Junit5 的形式不正确?

问题重现

首先我过后参考的是这篇文章: here

于是乎,我的依赖是这样的

<dependencies>    <dependency>        <groupId>org.junit.platform</groupId>        <artifactId>junit-platform-launcher</artifactId>        <version>1.7.1</version>        <scope>test</scope>    </dependency>    <dependency>        <groupId>org.junit.jupiter</groupId>        <artifactId>junit-jupiter-engine</artifactId>        <version>5.7.1</version>        <scope>test</scope>    </dependency>    <dependency>        <groupId>org.junit.vintage</groupId>        <artifactId>junit-vintage-engine</artifactId>        <version>5.7.1</version>        <scope>test</scope>    </dependency></dependencies>

而后,我在 src/test/java 中创立了 FirstTest.java

import org.junit.Assert;import org.junit.Test;import org.junit.jupiter.api.DisplayName;import java.util.ArrayList;import java.util.List;public class FirstTest {    @Test    @DisplayName("首次测试")    public void first_test() {        List<String> list = new ArrayList<String>();        list.add("张三");        list.add("李四");        list.add("王五");        Assert.assertEquals(3, list.size());    }}

执行后果,本想看看 Junit5 的 DisplayName 的成果,然而后果却和 Junit4 统一?Junit5 的 @DisplayName 齐全没起作用啊?

起因竟在 import

找了良久,我才发现问题的所在。起因居然是导入 @Test 包谬误

√ 正确导入示例: import org.junit.jupiter.api.Test;

× 谬误导入示例: import org.junit.Test;

结语

这个问题,过后花了将近 10 min 才找到答案。感觉还是对 Junit5 的三个子项目 JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage 了解不深。
其实,我还发现新建一个 Java8 编译的 Maven 我的项目时,只须要依赖 Jupiter 就够了。

<dependencies>    <dependency>        <groupId>org.junit.jupiter</groupId>        <artifactId>junit-jupiter-engine</artifactId>        <version>5.7.1</version>        <scope>test</scope>    </dependency></dependencies>

如果只依赖 Jupiter, 就不会援用到 Junit4 的 org.junit.Test 了,也就不会呈现这个低级谬误了。