Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 564845

Summary: [JUnit] Run as Junit test fails for Junit4 tests
Product: [Eclipse Project] JDT Reporter: Albert Tregnaghi <albert.tregnaghi>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: RESOLVED NOT_ECLIPSE QA Contact:
Severity: major    
Priority: P3 CC: jarthana, noopur_gupta
Version: 4.16   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Whiteboard:
Attachments:
Description Flags
Demo spring boot application none

Description Albert Tregnaghi CLA 2020-07-01 10:15:30 EDT
Today I upgraded to eclipse 2020-06 and started a JUnit 4 test for my spring boot application by doing a "Run as ... junit test" as usual. There was no launch configuration, so eclipse created one - as usual - with Junit5 runner as default. So far so good. Before upgrade this worked out of the box.

But now the new created launch configuration did not work. Also already existing and formerly working test launch configurations do no longer work.

Problem was the test did not show up. So looking at the console I found that the problem is junit platform is not found (see stacktrace below) - what is okay, because in my project I do not have junit5 dependencies, but old implementation (2019-09) did automatically launch with Junit4 runner!

A workaround is to go to every test launch configuration and manually change the Junit Runner to "Junit 4" ... but this is slow and really enerving. 

I restarted eclipse but behavior is stil the same. 
This is really disappointing, because it was much better in 2019-09. I regret doing the udpate without making a copy of my old installation.

```
java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: org/junit/platform/engine/EngineDiscoveryListener
	at org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.getLauncherDiscoveryListener(LauncherDiscoveryRequestBuilder.java:241)
	at org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.build(LauncherDiscoveryRequestBuilder.java:235)
	at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createTest(JUnit5TestLoader.java:94)
	at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.loadTests(JUnit5TestLoader.java:48)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:526)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: java.lang.NoClassDefFoundError: org/junit/platform/engine/EngineDiscoveryListener
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	... 8 more
Caused by: java.lang.ClassNotFoundException: org.junit.platform.engine.EngineDiscoveryListener
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	... 20 more
```
Comment 1 Jay Arthanareeswaran CLA 2020-07-01 13:07:43 EDT
Moving to UI where Junit support belongs.
Comment 2 Noopur Gupta CLA 2020-07-02 02:37:17 EDT
I do not see this issue on upgrade in Eclipse SDK using only JDT JUnit support. The JUnit version is correctly updated to use JUnit 5.6 container in a simple Java project.

Are you using Maven or Gradle? 

Which JUnit version is being used there?
Comment 3 Albert Tregnaghi CLA 2020-07-02 08:37:30 EDT
Created attachment 283480 [details]
Demo spring boot application

A simple demo application using spring boot + old junit4 test - you must call ./gradlew cleanEclipse eclipse before importing
Comment 4 Albert Tregnaghi CLA 2020-07-02 09:01:11 EDT
## My tries to reproduce the problem
### Create a simple demo app
I created a simple demo application and uploaded it to this issue.

The strange thing is, this is a similar setup as in my real application and when I do
./gradlew cleanEclipse eclipse 
+ 
import existing projects

#### Not reproducable
And call "run as Junit test" on IamJunit4Test.java this works inside a new workspace and also inside my existing workspace where I have got the problems (both 2020-06 workspace format)


### Check if this was a conversion error of workspace
I thought about a conversion problem from workspace 2019-12 to 2020-06

### Import demo application in a fresh 2019-12 workspace
So I downloaded a 2019-12 eclipse Java IDE and created a new workspace there.

imported demo application into the 2019-12 workspace and then started Junit4 test. All working.

### Start 2020-06 eclipse with created 2019-12 workspace and let convert
After starting former created workspace with 2020-06 and applying the conversion dialog I tried to start the existing launch configuration again.

and... It is working !?!

### Mixed up demo project with junit5 jupiter test
created a new jupiter (junit5) test

- then started junit5 test (worked)
- then started all tests inside project (worked)
- then started junit4 test alone (worked)
Comment 5 Noopur Gupta CLA 2020-07-02 09:08:38 EDT
This looks like the Gradle/buildship issue mentioned in bug 561797 which is being tracked in https://github.com/eclipse/buildship/issues/980.

Please check the workarounds mentioned in those bug reports until the problem is resolved.

*** This bug has been marked as a duplicate of bug 561797 ***
Comment 6 Albert Tregnaghi CLA 2020-07-02 09:34:01 EDT
As shown in my last comments I was ble to reproduce by given spring demo application.

But creating a new workspace 2020-06 and importing my existing projects (inclusive .gradlew cleanEclipse eclipse) did not fix my problems.

## Final solution

### tl;dr
In build.gradle add next line:
testImplementation 'org.junit.platform:junit-platform-launcher'

### Long story
I searched around and found following:

https://github.com/spring-projects/sts4/issues/431
(I am currently still using spring boot 2.2.x which brings up an older JUnit5 dependency which is not compatible with new Junit5 launcher in eclipse - demo app did use newer spring boot... thats the point...)

So the correct solution is to add
testImplementation 'org.junit.platform:junit-platform-launcher'
into your gradle build when you are using an older spring boot version than 2.3.0!

There is already a discussion with eclipse team (at least I hope so).


Having this solved for myself I close this issue
Comment 7 Albert Tregnaghi CLA 2020-07-02 09:36:13 EDT
I fixed the problem for my situation by adding
testImplementation 'org.junit.platform:junit-platform-launcher'

into my gradle build. The problem is discussed at
https://github.com/eclipse/buildship/issues/980