1

I am trying to run basic JUnit5 program. Using Eclipse Oxygen.3a(4.7.3a) with OpenJdk9. Getting error as below -

java.lang.NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.<init>(JUnit5TestLoader.java:31)
.
.
.
Caused by: java.lang.ClassNotFoundException: org.junit.platform.launcher.core.LauncherFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)

I had a look at this - Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory But since my IDE is up-to-date not sure what I am missing now.

My code is as below :

Main Class

package io.javabrains;
public class MathUtils {
    public int add(int a, int b) { return a + b;}
}

TestClass

package io.javabrains;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class MathUtilsTest {
    @Test
    void test() {
        System.out.println("This test ran successfully");
        //fail("Not yet implemented");
    }
}

POM

<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>io.javabrains</groupId>
  <artifactId>junit-5-basics</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>junit-5-basics</name>


  <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.jupiter.version>5.4.0</junit.jupiter.version>
  </properties>

   <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.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Siddhesh
  • 33
  • 2
  • 8
  • 1
    To be honest your IDE is not up-to-date cause it's version 4.7 whereas the recent version is 4.12 or 2019-09 ...apart from that have you checked if the build works on plain command line? – khmarbaise Sep 18 '19 at 18:37

2 Answers2

0

Eclipse and JUnit can sometimes clash across the moving versions

  • To fix your example, add JUnit 5 to your classpath IN ECLIPSE
  • Maven will run and pass as it does not use the missing launcher
  • Eclipse needs the sub-library that has the Launcher
  • CAVEAT: Its better to add the missing library to the pom as the problem persists in cross IDE projects (not everyone uses eclipse)
-1

Add following in pom.xml and try

Get "JUnit Platform Launcher" [version 1.4.0] maven command from mvnrepository.com and Add following in pom.xml and try