Eclipse JUnit 5 support

17,961

Solution 1

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

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

Solution 2

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

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 of the project is as follows:

<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>

Solution 3

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!

Share:
17,961
RoiEX
Author by

RoiEX

Web- and java-developer Also a Gamer.

Updated on July 08, 2022

Comments

  • RoiEX
    RoiEX almost 2 years

    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.

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

  • RoiEX
    RoiEX over 7 years
    I know that... I already mentioned it in my question. You may have missed the without part
  • Arpit Aggarwal
    Arpit Aggarwal over 7 years
    @RoiEX in your question you mentioned @RunWith(PlatformRunner.class) which I corrected to @RunWith(JUnitPlatform.class)