Reputation: 2398
Whenever I run my projects JUnit test (using JUnit 5 with Java 9 and Eclipse Oxygen 1.a) I encounter the problem that eclipse can't find any tests.
Under the run configuration, eclipse can't even find the method which is annotated with @Test, but instead only shows me "(all methods)". The following picture hopefully gives a better glimps of my setup:
java.lang.NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.<init>(JUnit5TestLoader.java:31)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.base/java.lang.Class.newInstance(Unknown Source)
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.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 11 more
I've already tried
Some of these steps can be found here, but in the end the problem remained.
Upvotes: 133
Views: 306282
Reputation: 1
I was facing the same issue. The issue was due to , the test class was created as New-> Class. Instead it has to be New -> Junit test case
Upvotes: 0
Reputation: 229
If you use beforeAll, beforeEach methods, remove them and then run. If you dont get any errors, you can change your the methods static situation.
In my case, I had used beforeAll without static key and then i used static key for method signature and my problem was solved.
Upvotes: 0
Reputation: 1
Change it to JUnit 4 And its works. Please try it (If you have simple example then definatlty try this.) Click on that class file --> It will come Run/Debug Settings ---> Click on new --> Select JUnit --> Click on okay --> Change TestRunner into Junit 4. And Click on apply and okay. And then run It will work for sure.
Upvotes: 0
Reputation: 131
I use actually spring-tool-suite-4-4.5.1 and I had this bug when I want run a test class. and the solution was to add to 'java build path', 'junit5' in Libraries
Upvotes: 12
Reputation: 9708
I went through painfully one by one on the existing answers and none of them applied to me and none of them fixed it for me.
In my case, under Project Properties and the Source tab, I had added my test folder as a source path. There is a subproperty under this entry that says Output Folder
. I set it to where I wanted my test class files to be generated and they were saved at this location.
However, JUnit 5 which is the Jupiter 5.7.0 version (and this is the one that comes with eclipse not a separate mvn dependency I added to my project) when run is actually looking in a different folder than I specified, this being target/test-classes
. If I manually move my class files from the Output Folder
folder that I specified to the target/test-classes
folder, then my unit tests will run.
So make sure that your class files are in the location that the runtime thinks they should be in.
Upvotes: 0
Reputation: 61
@Test annotation must be imported from org.junit.jupiter.api.Test so the Junit5 can read it. Junit4 use @Test annotations imported from org.junit.Test package.
Upvotes: 4
Reputation: 49
Converting the Java project to Maven Project fixed the issue for me. The conversion was done on Eclipse by: Right Click Project -> Configure -> Convert to Maven Project
Upvotes: 1
Reputation: 15824
You should make sure your test case function is public rather than private to make it accessible by Test Runner.
@Test
private void testmethod(){}
to
@Test
public void testmethod(){}
Upvotes: 2
Reputation: 61
replace:
import org.junit.Test;
with:
import org.junit.jupiter.api.Test;
Upvotes: 6
Reputation: 155
Removing the JUnit library, and then adding it again fixed this issue for me.
Upvotes: 0
Reputation: 2290
You might be importing @Test
from org.junit.Test
, which is a JUnit 4 annotation. The Junit5 test runner will not discover it.
The Junit5 test runner will discover a test annotated with org.junit.jupiter.api.Test
Found the answer from Import org.junit.Test throws error as "No Test found with Test Runner "JUnit 5""
Upvotes: 2
Reputation: 1
I got the same problem in a SpringBoot project and, in my case, the cause was that the ouput folder '/target/test-classes' was empty.
Fixed it manually in the .classpath file, looking for the classpathentry with the attribute "test" and changing the content of the output attribute:
From this:
<classpathentry kind="src" output="<project folder>/target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
to this:
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
Upvotes: 0
Reputation: 1
adding org.junit.platform to your pom and build it. Next, you need to go to "Run Configuration" of your TEST FILE and add JUNIT in the classpath, APPLY->RUN will resolve the issue.
Upvotes: 0
Reputation: 13
I had a similar issue. It was due to an incorrect source path in properties. Open project properties -> Java Build Path -> Sources. And remove the incorrect path if it is your case
Upvotes: 0
Reputation: 1397
Try to reconfigure your unit test configurations to be the project JRE.
Upvotes: 1
Reputation: 1
This issue happening while implementing Mockito with spring boot api. Below add below dependency in in gradle file
compile group: 'org.mockito', name: 'mockito-core', version: '2.22.0'
Note: Spring boot version 2.2.0
Upvotes: 0
Reputation: 81
I also faced the same issue you just need to add the library , Junit Library is already provided along with Eclipse so you just need to follow below
Build Path > Configure Build Path > library > Add library > JUnit > Next > finish
It works for me
Upvotes: 8
Reputation: 713
I copied some code and created a TC. Basically converted a normal java class to JUnit TC. I was gettgig error. Later I created a new JUnit Class from New -> Junit TC. Copied the same code. It works fine and error vanished.
Upvotes: 0
Reputation: 591
SIMPLE FIX: (Add JUnit 5 Library)
INSTRUCTIONS:
Upvotes: 49
Reputation: 31
You are missing JUnit 5 platform launcher with group: 'org.junit.platform', name: 'junit-platform-launcher'
Just add in ur POM:
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
</dependency>
Upvotes: 3
Reputation: 41
you should know that :
@Before from junit4 goes with @Test : "import org.junit.Test"
AND
@BeforeEach from Junit5 goes with : "import org.junit.jupiter.api.Test"
so make sure you are using the imports from the same version of Junit , otherwise it w'ont Work I guess.
Upvotes: 4
Reputation: 84
When I change my jupiter api version into latest one, it was solved. Meanwhile, my eclipse and other eclipse extensions ide's (such as STS) is getting build path error. For every maven update, ide forces me to set the JRE System Library.
Upvotes: 0
Reputation: 1
It may cause by the version of junit-platform-launcher / junit-platform-runner.
1.1.0 does not work
1.4.1 works in my setup.
I think this is a bug in eclipse as it is working with higher version libraries of junit and not other version.
This resolved my issue. The other resolution looked less feasible or risky to me. Thanks.
Upvotes: 0
Reputation: 11
I faced this issue with Eclipse (Version: 2019-12 (4.14.0)) too. The solution seems either to use IntelliJ or to use the Maven test to run such tests in Eclipse.
Upvotes: 1
Reputation: 1
Just added my Test.class via eclipse menu and worked. Right click to project -> New -> Other -> JUnit -> JUnit Test Case
Upvotes: 0
Reputation: 180
I'm using:
Spring Tool Suite 4 Version: 4.4.2.RELEASE Build Id: 201911201053 Java version: 1.8.0_191
and the message displayed is No tests found with test runner 'JUnit5'
what worked for me was the configuration below
<properties>
<java.version>1.8</java.version>
<junit-jupiter.version>5.5.2</junit-jupiter.version>
<junit-platform.version>1.5.2</junit-platform.version>
</properties>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
Upvotes: 2
Reputation: 9210
I ran into the same error, and in my case it was a simple matter of going to Project Properties > Maven > Update project
and/or cleaning and rebuilding the project.
Upvotes: 0
Reputation: 11
For me, I configured the build path to add JUnit 5 Library and also by adding the dependency
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
seperately.
Upvotes: 1
Reputation: 21
you should change
@Test
public static void testmethod(){}
to
@Test
public void testmethod(){}
the @Test is unsupport static method
Upvotes: 1
Reputation: 209
You can use only junit-jupiter
as a test dependency instead of junit-jupiter-api
, junit-platform-launcher
, junit-jupiter-engine
.
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
Upvotes: 2