Eclipse JUnit 5 支持

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38402155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:14:40  来源:igfitidea点击:

Eclipse JUnit 5 support

javaeclipseunit-testingjunitjunit5

提问by RoiEX

Currently, JUnit 5 is just out with a "stable" version. IntelliJ supports JUnit 5 according to the Website. My question is if eclipse is supporting JUnit 5 as well, and if not when it is going to be supported. With supported I mean if I can run JUnit 5 tests without the need for a @RunWith(PlatformRunner.class)annotation.

目前,JUnit 5 刚刚发布了一个“稳定”版本。根据网站,IntelliJ 支持 JUnit 5。我的问题是 Eclipse 是否也支持 JUnit 5,如果不支持,何时支持。支持是指我是否可以在不需要@RunWith(PlatformRunner.class)注释的情况下运行 JUnit 5 测试。

EDIT October 2017: Eclipse now officially supports JUnit 5as of Eclipse Oxygen1.a (4.7.1a)

编辑 2017 年 10 月:从 Eclipse Oxygen1.a (4.7.1a) 开始,Eclipse 现在正式支持 JUnit 5

采纳答案by Phoenix

You can run JUnit 5tests in Eclipse 4.7 Oxygenafter installing the JUnit 5 Support (BETA) for Oxygen 4.7plugin that you can find in the Eclipse Marketplace. After a complete Eclipse restart, open your project properties at

安装了JUnit 5 Support (BETA) for Oxygen 4.7插件后,您可以在Eclipse 4.7 Oxygen 中运行JUnit 5测试,该插件可以在 Eclipse Marketplace 中找到。完成 Eclipse 重新启动后,在以下位置打开您的项目属性

Java Build Path -> Libraries -> Add Library -> JUnit -> JUnit 5

Java 构建路径 -> 库 -> 添加库 -> JUnit -> JUnit 5

回答by Arpit Aggarwal

With Eclipse Kepler Service Release 2, you can run JUnit 5tests after annotating the class with @RunWith(JUnitPlatform.class)as follows:

使用Eclipse Kepler Service Release 2,您可以JUnit 5在对类进行注释后运行测试@RunWith(JUnitPlatform.class),如下所示:

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
public class Junit5Demo {

    @DisplayName("First Test")
    @Test
    void firstTest() {
        assertEquals(1, 1);
    }
}

enter image description here

在此处输入图片说明

pom.xmlof the project is as follows:

pom.xml项目情况如下:

<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>arpit.aggarwal.junit5.demo</groupId>
    <artifactId>junit5-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <junit.jupiter.version>5.0.0-M2</junit.jupiter.version>
        <junit.vintage.version>4.12.0-M2</junit.vintage.version>
        <junit.platform.version>1.0.0-M2</junit.platform.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <includes>
                        <include>**/Test*.java</include>
                        <include>**/*Test.java</include>
                        <include>**/*Tests.java</include>
                        <include>**/*TestCase.java</include>
                    </includes>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>${junit.platform.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>${junit.vintage.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <version>${junit.platform.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

回答by Aaron Waddell

quick note: make sure you are using the jupiter Test annotation (org.junit.jupiter.api.Test) and not the old junit.test (junit4). I wasted several hours trying to get my test to run on junit 5 because of this!

快速说明:确保您使用的是 jupiter Test 注释 (org.junit.jupiter.api.Test) 而不是旧的 junit.test (junit4)。因此,我浪费了几个小时试图让我的测试在 junit 5 上运行!