【问题标题】:Eclipse JUnit 5 supportEclipse JUnit 5 支持
【发布时间】:2016-11-19 00:29:58
【问题描述】:

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

2017 年 10 月编辑:Eclipse 现在 officially supports JUnit 5 从 Eclipse Oxygen1.a (4.7.1a) 开始

【问题讨论】:

  • @Jägermeister 我已经读过那篇文章了……问题是当 JUnit 5 仍处于 alpha 阶段时发布了这篇文章 -> 根本不支持……现在 IntelliJ 支持它。
  • 你可能想关注Eclipse Bug 488566
  • 我投票结束这个问题,因为 Stack Overflow 不是产品支持邮件列表。
  • 一旦问题存在就关闭它有什么意义?它不会被删除,它会继续在谷歌搜索中出现。

标签: java eclipse unit-testing junit junit5


【解决方案1】:

安装 JUnit 5 Support (BETA) for Oxygen 4.7 插件后,您可以在 Eclipse 4.7 Oxygen 中运行 JUnit 5 测试,您可以在 Eclipse 市场中找到。完全重启 Eclipse 后,在

处打开您的项目属性

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

【讨论】:

    【解决方案2】:

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

    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);
        }
    }
    

    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>
    

    【讨论】:

    • 我知道......我已经在我的问题中提到了它。你可能错过了没有的部分
    • @RoiEX 在您提到的问题中您提到了@RunWith(PlatformRunner.class),我将其更正为@RunWith(JUnitPlatform.class)
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      升级

      我为此挣扎了一段时间。我想为使用 junit 5 测试的项目做出贡献,但我使用的是旧版本的 Eclipse Neon。

      我可以访问另一台运行 junit 5 测试的机器。似乎更新版本的 Eclipse 带有 junit 5 功能。我去了: https://www.eclipse.org/eclipseide/

      安装较新的版本,然后从 github 加载项目解决了这个问题。我发现在另一台计算机上它的版本为 2021-06,但通过简单的升级(启用了主要升级)将其升级到 2021-12,这与撰写此答案时的最新版本相匹配。

      我预计这将帮助我在未来管理更新和升级时不会遇到问题。

      自 2021 年夏季以来,将 Eclipse 与 GitHub 集成的方法发生了变化。请参阅此帖子。

      how to - github two factor authentication with eclipse

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-14
        • 2018-09-22
        • 2019-01-30
        • 2019-05-23
        相关资源
        最近更新 更多