35

I've just upgraded to Eclipse 2018.09 (the problem also occurs on Eclipse Photon), but it doesn't play nicely with a project that uses JUnit 4. The new version appears to run all tests using a JUnit 5 runner by default, which fails with the following errors because I only have JUnit 4 on the project's classpath, not JUnit 5:

Could not run test. No tests found with test runner 'Junit 5'.

java.lang.NoClassDefFoundError: org/junit/platform/engine/EngineExecutionListener
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:59)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.<init>(JUnit5TestLoader.java:34)
    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:370)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:365)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:309)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:224)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:208)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.engine.EngineExecutionListener
    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)
    ... 12 more

I want to be able to conveniently run my tests in this project without upgrading to a new major version of JUnit with a workaround I don't have to constantly repeat. I'm thinking that this will require somehow changing the default test runner.


Workarounds I've discovered, and why I'm not satisfied by them:

  1. Manually update every run configuration for every test to use the JUnit 4 runner. I'm not satisfied with this because it's annoying and a lot of work. I cannot find a way to change the JUnit run configuration defaults to always use the JUnit 4 runner. There's some promising-seeming stuff about "prototype" run configurations, but all the options are greyed-out and nonfunctional for JUnit configurations.
  2. Add JUnit 5 to the classpath. This allows the JUnit 5 runner to work, but I'm not satisfied with this because I shouldn't be forced to upgrade on account of my IDE.

Eclipse Photon version info:

Eclipse Java EE IDE for Web Developers.
Version: Photon Release (4.8.0)
Build id: 20180619-1200
OS: Windows 7, v.6.1, x86_64 / win32
Java version: 1.8.0_171

"Check for Updates" shows no relevant packages to install.

Eclipse 2018.09 version info:

Eclipse Java EE IDE for Web Developers.
Version: 2018-09 (4.9.0)
Build id: 20180917-1800
OS: Windows 7, v.6.1, x86_64 / win32
Java version: 1.8.0_171
Kaypro II
  • 3,210
  • 8
  • 30
  • 41
  • Before you comment, remember I don't want to solve this issue by upgrading to JUnit 5. – Kaypro II Oct 29 '18 at 15:55
  • Before you ask an Eclipse question, make sure your Eclipse IDE is up to date. ;-) Please upgrade to Eclipse 2018-09 (4.9). – howlger Oct 29 '18 at 16:07
  • 1
    The same problem occurs on a new install of Eclipse 2018-09 (4.9). – Kaypro II Oct 30 '18 at 20:40
  • Thanks for upgrading. Please show the content of your `.classpath` file (which is visible in the _Navigator_ view). Eclipse supports both JUnit 4 and JUnit 5, so I guess there's something wrong with your classpath. – howlger Oct 31 '18 at 08:05
  • 2
    Having same problem. Not sure classpath is going to help. I am having mvn driven project. As such its not about support, its about setting default junit version at the IDE or workspace level. – AmitM Nov 21 '18 at 11:06
  • 2
    I had the same problem with mvn project that was caused by junit-jupiter-api which pulled in junit5. after removing `org.junit.jupiterjunit-jupiter-api` all was fine for me. – k3b Nov 30 '18 at 10:57
  • 1
    After lots of fighting with this problem, I found that removing the jupiter dependency was answer that helped me. k3b please put it as an answer – Osama Abdulsattar Aug 26 '19 at 04:13

4 Answers4

9

I also stumbled upon this issue today. It seems that eclipse uses the JUnit 5 runner by default if JUnit 5 dependencies are found on the classpath.

In case of my maven project, investigating the dependency hierarchy showed that actually both JUnit 4 and JUnit 5 dependencies were on the classpath. JUnit 4 as the test dependency I added myself, JUnit 5 as a transitive dependency which I inherited from another library I relied upon. After excluding JUnit 5 from that dependency, eclipse again uses the JUnit 4 runner by default.

Maybe you could check if your project also has an unwanted dependency to JUnit 5 you are not aware of?

cortmann
  • 91
  • 1
  • 1
  • 1
    I've always found the way Eclipse shows dependencies quite clunky (not even alphabetically sorted!). Do you have any advice on how to investigate the dependency hierarchy? – vmaldosan Feb 17 '20 at 16:37
  • 1
    In case someone else looks for this feature, there's literally Dependency Hierarcy tab on the bottom of the editor, when you edit pom.xml in Eclipse. Along with Overview, Dependencies and Effective POM tabs. You can even click and exclude any dependency through this GUI instead of typing. – vitrums Mar 30 '22 at 18:16
5

In my case the project was created with Spring Initializr and it had automatically added the following exclusion to the spring-boot-starter-test dependency:

<exclusion>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
</exclusion>

Removing it fixed the problem for me.

InsertKnowledge
  • 1,012
  • 1
  • 11
  • 17
0

I faced the same issue now, and got it fixed after adding the Vintage Engine dependency in the POM.xml file. Try it out.

MVN Repository: https://mvnrepository.com/artifact/org.junit.vintage/junit-vintage-engine

  • 7
    Thanks, but I consider that a workaround. I do not think I should have to modify the classpath of my project on account of the IDE. Especially since the IDE is perfectly happy to run tests with the JUnit 4 runner (it just makes it annoying and impractical). – Kaypro II Mar 14 '19 at 22:38
0

The exclusive addition of JUnit 5 by the Spring initializer was the cause of my issues....commented out this from the pom.xml