4

I want to try out Junit 5. I'm running Oxygen 4.7.1a. I have a project with the following maven dependency:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.0.1</version>
    <scope>test</scope>
</dependency>

When I run a junit 5 test, I get the following error:

java.lang.NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.<init>(JUnit5TestLoader.java:31)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:368)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:363)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:307)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:222)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.launcher.core.LauncherFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 11 more

Digging through Stack Overflow and other sites, I'm still not quite sure what I'm missing. I'm using Java 8 if that makes a difference.

David Pérez Cabrera
  • 4,960
  • 2
  • 23
  • 37
Jason Thompson
  • 4,643
  • 5
  • 50
  • 74
  • Can you post you full maven file? If you look at https://github.com/junit-team/junit5-samples/tree/master/junit5-vanilla-maven, you have what is needed. So can you provide more information. May be your eclipse project is created as a Java Project and does not have maven nature enabled to pull the dependencies from Maven. – randominstanceOfLivingThing Oct 18 '17 at 15:28
  • 1
    You only have included the api. Include the engine instead (see the link of randominstanceOfLivingThing). – dunni Oct 18 '17 at 15:29
  • The stack trace is the same as of [_Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError_](https://stackoverflow.com/questions/46717693/eclipse-no-tests-found-using-junit-5-caused-by-noclassdeffounderror). Are you sure you do not use Java 9? Could you show your Java build path of the project? – howlger Oct 18 '17 at 16:30
  • @howlger The project itself is JDK 1.8 and I don't have JDK 1.9 as a "installed JRE". Though there is a chance that eclipse itself is using a Java 9 JRE since I installed it the other day. Would that make a difference? – Jason Thompson Oct 18 '17 at 18:00
  • I double checked and eclipse itself is running via Java 8. So the project is JDK 1.8 and the IDE is running on the Java 8 jvm. – Jason Thompson Oct 18 '17 at 18:29
  • @dunni, I just swapped out api for the engine and I get the same result. – Jason Thompson Oct 18 '17 at 18:31
  • @randominstanceOfLivingThing I don't use use m2e. I use the maven eclipse plugin to download the artifacts to my local m2 repo and create the appropriate eclipse project. – Jason Thompson Oct 18 '17 at 18:40
  • Did you run mvn eclispe:eclipse or use import an existing maven project? – randominstanceOfLivingThing Oct 18 '17 at 18:56
  • Hi @randominstanceOfLivingThing, I use eclipse:eclipse. I was able to figure out a solution to the problem and posted the answer below. – Jason Thompson Oct 18 '17 at 18:57

1 Answers1

2

I went ahead and had eclipse add the libraries itself to a sample non-maven project to see what I was missing. After some experimenting, here is the minimum dependencies for a pom that are needed to run junit tests in eclipse.

Please note, this does not automatically make junit tests work in maven surfire, just eclipse.

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.0.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.0.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>1.0.1</version>
        <scope>test</scope>
    </dependency>

Yes I'm fully aware that junit-jupiter-engine pulls in junit-jupiter-api transitively, but since I use it in a test compile time way, it is good practice to include it.

Jason Thompson
  • 4,643
  • 5
  • 50
  • 74