Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Eclipse 2018.09 to use the JUnit 4 test runner by default?

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 
like image 785
Kaypro II Avatar asked Oct 29 '18 15:10

Kaypro II


People also ask

What is default JUnit runner?

If a test class doesn't have this annotation, JUnit 4 runs it by using the default test runner that is called the BlockJUnit4ClassRunner . A test class can use only test runner.


2 Answers

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?

like image 61
cortmann Avatar answered Oct 06 '22 01:10

cortmann


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.

like image 24
InsertKnowledge Avatar answered Oct 06 '22 01:10

InsertKnowledge