Eclipse JUnit 5 支持

2022-09-04 19:50:01

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

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


答案 1

在安装 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


答案 2

使用 ,您可以在对类进行批注后运行测试,如下所示:Eclipse Kepler Service Release 2JUnit 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.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>

推荐