Repository: iluwatar/java-design-patterns
Issue: JUnit5 test are not able to run in Eclipse
Labels: type: bug
info: good first issue
epic: build issue
epic: ide issue
epic: dependencies

Steps to recreate:

1. In Eclipse, select a unit test to run by right-clicking and selecting: "Run Configurations." Select a JUnit execution and ensure that the JUnit5 runner is selected:
![image](https://user-images.githubusercontent.com/1112973/66777789-ecb0e880-ee97-11e9-9bf2-ace877c11bd2.png)

1. Click the Run button. 

1. The following error is shown: 
![image](https://user-images.githubusercontent.com/1112973/66777844-179b3c80-ee98-11e9-8090-22953a3950f5.png)
and the following stack trace is displayed:

```
java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException
	at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createUnfilteredTest(JUnit5TestLoader.java:75)
	at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createTest(JUnit5TestLoader.java:66)
	at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.loadTests(JUnit5TestLoader.java:53)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.commons.PreconditionViolationException
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	... 7 more
```




Workaround: Run the mvn test on the command line and the tests will execute. 

According to the following Stackoverflow: https://stackoverflow.com/questions/57040675/java-lang-noclassdeffounderror-org-junit-platform-commons-preconditionviolation adding the following dependency to the project will fix. (I've tried with the example project and it seems to work). 

```
 <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-commons</artifactId>
            <version>1.5.2</version>
 </dependency>
```

Issue comments:
I got this at work today as well, and we solved it with the above suggested solution. I can prepare a PR for it if noone else already did it?
Sure @lbroman, PR is welcome
Instead of inserting `junit-platform-commons` in each of the ~120 module poms a better fix would be to just update JUnit 5 to the most current version...
Does this fix the problem?

I suggested in my PR (not visible here) that a better aproach might also be to upgrade the last junit 4 projects to jupiter and then pull the jupiter dependencies up to the root pom.
Yes it does, because JUnit 5.5.2 transitively pulls junit-platform-commons 1.5.2:

![jupiter-5 5 2](https://user-images.githubusercontent.com/110982/66992341-f5890080-f0c9-11e9-8756-edc81336beab.PNG)

Tried updating to 5.5.2 and it broke the maven build, current version dates back to 2017...
junit-platform-commons is in the current dependency tree also, but 1.0.2.

Also noticed I cannot reproduce this issue on Eclipse EE but only on Eclipse Java, so is problably eclipse edition related. Will test this by managing the junit-platforms-commons version instead and see what that might bring.
Updating to 5.5.2 works for me. In addition `junit-jupiter-api` have to be added in the parent pom because `spring-boot-dependencies` imports and old version of this. 

Now the tests run on the command line as well as from eclipse. I'll submit a pull request.

![command line](https://user-images.githubusercontent.com/110982/67011635-4ad80880-f0f0-11e9-948c-3e28e1cecfbe.PNG)

![eclipse](https://user-images.githubusercontent.com/110982/67011770-7eb32e00-f0f0-11e9-8d03-dbd78f76f7f5.png)


A  future improvement may be to remove all `junit-jupiter-*` dependencies and replace them by the aggregator dependency [`junit-jupiter`](https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter/5.5.2) which was introduces with 5.4.0.
Another option is to switch to TestNG.
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.createUnfilteredTest(JUnit5TestLoader.java:75)
	at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createTest(JUnit5TestLoader.java:66)
	at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.loadTests(JUnit5TestLoader.java:53)
	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(Unknown Source)
	at java.security.SecureClassLoader.defineClass(Unknown Source)
	at java.net.URLClassLoader.defineClass(Unknown Source)
	at java.net.URLClassLoader.access$100(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	... 9 more
Caused by: java.lang.ClassNotFoundException: org.junit.platform.engine.EngineDiscoveryListener
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	... 21 more


Pull request review comments:

Patch that resolved the issue:
diff --git a/pom.xml b/pom.xml
index 020c784c62fd..22dedc14a59e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -39,7 +39,7 @@
         <spring-data.version>2.0.14.RELEASE</spring-data.version>
         <h2.version>1.4.190</h2.version>
         <junit.version>4.12</junit.version>
-        <junit-jupiter.version>5.0.2</junit-jupiter.version>
+        <junit-jupiter.version>5.5.2</junit-jupiter.version>
         <junit-vintage.version>${junit.version}.2</junit-vintage.version>
         <sping-test-junit5.version>1.0.2</sping-test-junit5.version>
         <compiler.version>3.8.1</compiler.version>
@@ -242,6 +242,12 @@
                 <version>${junit.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>
             <dependency>
                 <groupId>org.junit.jupiter</groupId>
                 <artifactId>junit-jupiter-engine</artifactId>


Test patch: