129

The problem

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.

The description

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:

https://imgur.com/a/FTe9c

Console output:

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

What I tried so far

I've already tried

  • to remove the test folder from build path and add it again.
  • to start the test with hovering over the method annotated with @Test and then click "Run as JUnit Test".
  • remove JUnit from Buildpath and add it again
  • restart eclipse
  • I've also moved the project whole project from one machine to another machine and tried it with the provided eclipse installation there
  • to rename the test method.
  • to retype the @Test annotation

Some of these steps can be found here, but in the end the problem remained.

demongolem
  • 9,474
  • 36
  • 90
  • 105
ShadowDragon
  • 2,238
  • 5
  • 20
  • 32
  • But did you try to find out the difference as stated in the answer to this -https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java? – Naman Oct 13 '17 at 01:01

46 Answers46

173

I fixed the issue by right clicking the test and selecting 'Run Configurations' and changing the "Test runner:" selection to 'JUnit 4' as shown here:

Screenshot of run configruations

I ran the test again and it worked.

JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
stringVector
  • 1,923
  • 2
  • 12
  • 11
  • 17
    It's funny that selecting "JUnit4" in run configurations while using JUnit5. I won't call that a good solution that I can rely on while I'm dealing with a big project. – Levent Divilioglu Sep 18 '18 at 14:57
  • 2
    I also find this solution suspicious, it even lacks an explanation as to why this works and what risks one may take. – Amadán May 29 '19 at 09:24
  • 1
    If doing this didn't solve the issue for you then go to classpath tab on the same window and add JUnit to the Bootstrap Libraries. – Abhi Dec 03 '19 at 09:51
  • Thanks; selecting JUnit 4 resolved my issues. For now, it's the simplest way to resolve the issue. – Tes Apr 28 '20 at 00:20
  • In my opinion this less invasive solution, that is, without modifying the source code. – Rafael Rocha Jun 25 '20 at 09:05
  • This worked for me. My other projects ran tests fine but a new one did not. Exactly the same POMs with expected differences. Where are these run configurations stored anyway? Not in any files I could find. – Jack D Menendez Dec 04 '20 at 09:07
  • At a guess, this may only work if you follow the convention of naming test methods "test" — in which case it would be detected by the JUnit4 running as a fallback to the JUnit3 convention, which predated the availability of annotations (and thus had no @Test). – Joel Aelwyn Jul 09 '21 at 16:21
  • I got that popup (without stack trace) when I had both JUnit 4 and JUnit 5 in the classpath (during a spring-boot upgrade). My tests were developed with JUnit 4 but Eclipse selected JUnit 5. The solution was to remove JUnit 5 from the classpath and to delete the old JUnit Run Configurations. – Pino Feb 14 '22 at 11:45
76

I have the same issue with STS 3.9.1. It seems like an Eclipse bug, however, to fix this you can add a test dependency junit-platform-launcher to your project (https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher)

This is how I did for my project which uses gradle:

dependencies {
    // other stuff here

    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: "5.${junit5MinorVersion}"
    testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: "1.${junit5MinorVersion}"
}

gradle.properties file:

junit5MinorVersion=1.0

I believe the same applies if you see this exception while using IntelliJ IDEA.

Andrii Karaivanskyi
  • 1,942
  • 3
  • 19
  • 23
  • in which file would I add the dependency? – jvdh Feb 16 '18 at 08:48
  • 1
    If using gradle usually it goes to `build.gradle`. If you are using maven it's `pom.xml`. The format for maven is different though. – Andrii Karaivanskyi Feb 21 '18 at 21:54
  • 1
    That fixed it for me. Thanks! – Chris Feb 27 '18 at 17:51
  • 5
    Here's the pom.xml snippet to go inside : org.junit.jupiter junit-jupiter-engine 5.1.0 test org.junit.platform junit-platform-launcher 1.1.0 test – user118967 Apr 02 '18 at 06:50
  • This answer should be the accepted, rather than the one suggesting a separate test project! – Gerold Broser Aug 11 '18 at 12:54
  • 1
    Still adding junit-platform 1.1.0 did not work for me. It shows same message "No tests found with test runner using JUnit 5" " – Satish Patro Aug 26 '19 at 06:59
  • 3
    @PSatishPatro After adding the above. You need to go to run configuration for that Junit test case and then go to Classpath tab-> Advanced-> Add Library-> OK-> Junit-> Finish. Then go ahead and Run it.This will add the library during the execution of the Junit – Jaison Varghese Oct 29 '19 at 14:40
49

In my case, the problem was myself and no IDE like Eclipse. I've imported the JUnit 4 Test class.

So do NOT import this one:

import org.junit.Test  // JUnit 4

But DO import that one:

import org.junit.jupiter.api.Test // JUnit 5
armin.miedl
  • 1,070
  • 1
  • 11
  • 16
  • I was switching between JUnit 4 and 5 because the current project is using JUnit 5, but when reporting bugs to Hibernate etc. I need to switch to JUnit 4. I ended up accidentally mixing the imports. ‍♂️ – Kawu Jan 23 '20 at 21:31
  • This answer needs more upvotes! – Monish Sen Feb 28 '22 at 03:17
48

SIMPLE FIX: (Add JUnit 5 Library)

INSTRUCTIONS:

  • Right click on project -> Build Path -> Configure Build Path
  • In the pop-up -> Add Library -> JUnit -> JUnit 5 -> Finish -> Apply
  • You should see the JUnit 5 Library (and its jars) added to your project
  • Right click on project -> Maven -> Update Project -> OK
Jerry Carney
  • 581
  • 4
  • 3
  • GRADLE FIX: (Add JUnit 5 Library) INSTRUCTIONS: * Right click on project -> Build Path -> Configure Build Path * In the pop-up -> Add Library -> JUnit -> JUnit 5 -> Finish -> Apply * You should see the JUnit 5 Library (and its jars) added to your project * Right click on project -> Gradle -> Refresh Gradle Project * Right click on project -> Run As -> Junit Test – Larry Ricker Feb 03 '21 at 21:50
  • 3
    This should be accepted answer, thank you @JerryCarney – pixel May 03 '21 at 01:00
23

You ran into Eclipse bug 525948 which has already been fixed and which will be published in the upcoming release Oxygen.3 (4.7.3), March 21, 2018.

As workaround, put your test code in a separate project and add the project under test to the modulepath, but do not add a module-info.java to your test project. With your project, class and module naming, it should look something like this:

enter image description here

See also my video that shows Java 9 and JUnit 5 in Eclipse Oxygen.1a in action

howlger
  • 31,050
  • 11
  • 59
  • 99
  • *Put your test code in a separate project* do you mean defining another module for tests? – Naman Oct 13 '17 at 07:01
  • 1
    @nullpointer I mean to create a new Java project that is not modular (has no `module-info.java`). In Eclipse a Java project has a single output folder and therefor it can only contain one or none `module-info.java`. But you can add modules to the project's modulepath even though the project is not modular (has no `module-info.java`). – howlger Oct 13 '17 at 07:34
  • Not very clear from the answer. Could you share a sample structure/screenshot for the suggestion? – Naman Oct 13 '17 at 07:58
  • @nullpointer I need to correct myself: in Eclipse a Java project can have multiple output folders (one output folder per source folder). But this can not be used (at least I don't know how) for a workaround of this Eclipse bug (see my edited answer). – howlger Oct 13 '17 at 11:15
  • Okay. I would wait for the OP to confirm that this works. Also would love to see a sample if you could update in the answer for the workaround suggested. IMO, would add value to the answer for people looking for that alternate until the bug fix as pointed by you. :) – Naman Oct 13 '17 at 11:19
  • This workaround works for me. ;) Just had to figure out that eclipse added my data to the lib module path and not project module or classpath. Also, the link in the answer to the video doesn't work. Would be nice if this can be fixed. – ShadowDragon Oct 13 '17 at 12:04
  • @ShadowDragon Sorry for the broken link which should work now. – howlger Oct 13 '17 at 12:08
  • 1
    @ShadowDragon the drawback with this hack is that we need a different package name for the test classes, thus cant do package-private testing – kleopatra Nov 25 '17 at 07:05
  • 9
    I just installed Oxygen.3 (4.7.3) and the problem persists. – user118967 Apr 02 '18 at 06:37
  • @user118967 I tested it with Oxygen.3 and it works for me, as you can watch here: https://www.youtube.com/watch?v=uJNzMGrbXbI&t=42s Do you get the same problem with [Photon M6](http://www.eclipse.org/downloads/index-developer.php?photon)? Which error message do you get exactly? – howlger Apr 02 '18 at 09:49
  • I kept getting the same error as initially (same as the OP). However, I used Andrii's solution and it worked. Thanks for replying! – user118967 Apr 03 '18 at 15:42
  • 2
    @chomp If you have 2018-09, it's a different issue even if the symptoms look similar. If no tests were found, this is mostly due to an incorrect classpath/module path (_Project > Properties: Java Build Path_). – howlger Dec 10 '18 at 12:37
  • I had this issue with STS. My workaround was simply to remove the test class and then recreate it. – Ara Kokeba Dec 17 '19 at 15:34
  • @howlger you said it is fixed in upcoming release Oxygen.3 (4.7.3), March 21, 2018. I have eclipse version as "Version: Oxygen.3a Release (4.7.3a) Build id: 20180405-1200" but still I face same error. I am using Java 8. Also as suggested below by few programmers, when I "Run Configurations' and changing the "Test runner:" selection to 'JUnit 4' it works. But I don't want to do that. I want to run my Junit with Junit 5 only. Can anyone please suggest me correct solution. Thanks in advance. – Datta Jan 23 '20 at 08:57
  • @Datta You face a different issue. You are more than 2 years and 7 releases behind. The current Eclipse version is 2019-12 (4.14). Do not wast time by using outdated software. – howlger Jan 23 '20 at 09:32
  • 2
    @howlger Thanks. After upgrading to latest version this problem gone. Thanks again for your help :). – Datta Jan 24 '20 at 07:29
  • See answer from StringVector for the real solution. – pmartin8 Jan 29 '20 at 16:43
  • 1
    @pmartin8 My answer is verified as correct (see bug report and comments above) and is specific to Eclipse Oxygen.1a (4.7.1a) and Oxygen.2 (4.7.2) only. In contrast, the answer you mentioned refers to a different issue with the same or similar error message. – howlger Jan 29 '20 at 18:01
21

Answers so far did not adress the problem of sharing code with other people who don't necessarily use Eclipse. Here is one proposition. The key is to use a maven profile to solve the Eclipse Case.

It assumes you have defined a property junit5.version in your pom like:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit5.version>5.1.1</junit5.version>
</properties>

then in the profiles section add the following:

<profiles>
    <profile>
        <id>eclipse</id>
        <dependencies>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
            </dependency>
            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-launcher</artifactId>
            </dependency>
        </dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>${junit5.version}</version>
                    <scope>test</scope>
                </dependency>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-launcher</artifactId>
                    <version>1.1.1</version>
                    <scope>test</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </profile>
</profiles>

All you have to do after this is to select the profile in your local Eclipse: Right click on your project and select Maven > Select Maven Profiles... (or hit Ctrl + Alt + P), and then check the "eclipse" profile we just created.

Selection Of Maven Profile

With that you are done. Your Eclipse will run Junit 5 tests as expected, but the configuration you added won't pollute other builds or other IDE

Developer Guy
  • 2,318
  • 6
  • 19
  • 37
Aldian
  • 2,592
  • 2
  • 27
  • 39
16

Adding this maven dependency with JUnit Jupiter (v.5.5.1) solves the issue.

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.5.1</version>
    <scope>test</scope>
</dependency>
hbhattac
  • 161
  • 1
  • 3
11

FYI, another cause of "No tests found using junit5" is (inadvertently or intentionally) declaring the test cases "private":

// Example of test case that doesn't get included
@Test
private void testSomeMethod() {
}

They need to be public.

Mike B
  • 111
  • 1
  • 2
11

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

enter image description here

fatima fahmi
  • 121
  • 1
  • 4
  • I used this solution at first and it worked, then I've read the other answers and I've found that the "correct" one was [@armin.miedl's](https://stackoverflow.com/a/56050153/3514976). I removed the JUnit 5 library from the build path and replaced the Test annotation import with the correct one. – nonzaprej May 24 '21 at 10:21
10

Following Andrii Karaivanskyi's answer here's the Maven POM declaration:

<properties>
    ...
    <junit-jupiter.version>5.2.0</junit-jupiter.version>
    <junit-platform.version>1.2.0</junit-platform.version>
    ...
</properties>

<dependencies>
    ...
    <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>
    ...
</dependencies>

UPDATE

As per comment by Alexander Wessel you can use org.junit:junit-bom as described in his answer to question Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • 1
    Don't declare properties for the jupiter and platform versions, [use the BOM instead](https://junit.org/junit5/docs/current/user-guide/#dependency-metadata-junit-bom). Basically add a dependency on the BOM in the dependencyManagement tag with `scope=import type=pom`, then add dependencies to junit-platform-launcher and junit-jupiter-engine in test scope, _without giving a version_ in the dependencies section. The BOM will always ensure that you have only one version of JUnit Platform and Jupiter on classpath, and that the respective versions are the ones intended (e. g. 5.2.0 with 1.2.0). – Alexander Wessel Oct 30 '18 at 23:23
  • @AlexanderWessel I have no BOM. This is just an example excerpt which dependencies one has to declare with Maven, wherever this may be. – Gerold Broser Oct 31 '18 at 08:06
  • 1
    it's not wether you have (or rather your project has) a BOM itself. I was explaining that by _importing_ the BOM _that JUnit 5 provides_, you can ensure the Junit Jupiter and Junit Platform versions are compatible, and you won't need to declare a version in the individual dependencies, thus also no longer needing to declare the version properties. To be clear: Your example _does_ work fine. But importing the JUnit BOM is a bit more elegant (in my humble opinion) and, in some circumstances, safer. – Alexander Wessel Nov 05 '18 at 14:06
  • [Here's my answer showing the imported BOM approach verbatim.](https://stackoverflow.com/a/53172689/1845794) – Alexander Wessel Nov 06 '18 at 13:18
10

None of the solutions helped:

The problem is that Eclipse 2018-12 has support for JUnit 5.3.1. If you start it with a version before that, it will fail.

So make sure you use at least 5.3.1.

5.4.0 does work too.

In case your parent pom is Spring Boot, you need to make sure (in dependency management) that junit-jupiter-api is set to the same version. You don't need junit-platform-runner or -launcher!

T3rm1
  • 2,299
  • 5
  • 34
  • 51
  • 2
    Thanks for the tip. I am using 2019-3 and I added property `5.4.1` to override the version used in Spring boot (2.1.3.RELEASE). – Ritesh Mar 22 '19 at 19:02
  • 3
    This was the one that fixed it for me. I removed the -launcher dependency and it started working. (eclipse 2019-09 and junit 5.4.2) – Raymond Apr 24 '19 at 12:56
  • This solution works for me as well. However, in my case, I changed from 5.3.1 to 5.5.1 and the junit-platform-engine to 1.5.1. – Arwan Khoiruddin Jul 22 '19 at 17:46
  • 1
    This fixed it for me. In eclipse 2020-03 tests did not start with junit version 5.5.2 but with 5.6.0. As a general update for your answer: It might be necessary to use at least the junit version eclipse uses. – Lars Briem Apr 09 '20 at 12:09
8

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

Alok Ranjan
  • 81
  • 1
  • 1
6

From the start the error message tell you that class is not found : NoClassDefFoundError that mean the PATH to junit is the problem.

  1. Press right click to project folder and choose Properties OR select project folder and press combination cmd + i.

  2. select from list "Java Build Path".

  3. select "Libraries" tab
  4. If JUnit 5(or JUnit 4) is added to "Modulepath", select the "JUnit 5" and press Remove.
  5. select "Classpath", press "Add Library...".
  6. from opened "Add Library" window, select JUnit, press next.
  7. Select JUnit library version that you need and press Finish.

That is all. Try to run test again.

Ion Timotin
  • 71
  • 1
  • 3
6

replace:

import org.junit.Test;

with:

import org.junit.jupiter.api.Test;
atline
  • 28,355
  • 16
  • 77
  • 113
Vikash Kumar
  • 61
  • 1
  • 1
4

I got the same problem after creating a new TestCase: Eclipse -> New -> JUnit Test Case. It creates a class without access level modifier. I could solve the problem by just putting a public before the class keyword.

du-it
  • 2,561
  • 8
  • 42
  • 80
  • Both class and method required public keyword. https://junit.org/junit4/javadoc/4.12/org/junit/Test.html – hmehandi Sep 15 '19 at 07:35
4

As everyone informed it's IDE bug, I tried in Eclipse and STS. In both the cases, it is failing.

As a workaround, I have fixed by modifying the pom.xml file like below.

I have added these two maven dependencies junit-jupiter-engine and junit-platform-launcher.

pom.xml

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit-jupiter.version}</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform launcher -->
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>${junit-platform.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Also please make sure to add the version of both the maven dependencies in the properties tag.

<properties>
    <java.version>1.8</java.version>
    <junit-jupiter.version>5.2.0</junit-jupiter.version>
    <junit-platform.version>1.2.0</junit-platform.version>
</properties>
Arvind Katte
  • 995
  • 2
  • 10
  • 20
4

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.

Y.halaoui
  • 41
  • 2
4

@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.

3

Using STS 3.9.1 I got the same problem. However, currently I do not require any new JUnit5 features, so I tried to force using an older version. If using maven, you can add following dependencies to your pom.xml:

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>${junit.platform.version}</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>${junit.jupiter.version}</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>${junit.vintage.version}</version>
    <scope>test</scope>
</dependency>

This did the trick for me (at least as long as I do not need JUnit5 explicitly).

mahe
  • 31
  • 3
3

I am using eclipse 2019-09.

I had to update the junit-bom version to at least 5.4.0. I previously had 5.3.1 and that caused the same symptoms of the OP.

My config is now:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>5.5.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
Lump
  • 31
  • 4
3

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>
2

Since it's not possible to post code blocks into comments here's the POM template I am using in projects requiring JUnit 5. This allows to build and "Run as JUnit Test" in Eclipse and building the project with plain Maven.

<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>group</groupId>
    <artifactId>project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>project name</name>

    <dependencyManagement>
      <dependencies>
        <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit-bom</artifactId>
        <version>5.3.1</version>
        <type>pom</type>
        <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>

    <dependencies>
      <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <scope>test</scope>
      </dependency>

      <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <scope>test</scope>
      </dependency>

      <dependency>
        <!-- only required when using parameterized tests -->
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <scope>test</scope>
      </dependency>
    </dependencies>
</project>

You can see that now you only have to update the version in one place if you want to update JUnit. Also the platform version number does not need to appear (in a compatible version) anywhere in your POM, it's automatically managed via the junit-bom import.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
2

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>
RobC
  • 22,977
  • 20
  • 73
  • 80
Aly Radwan
  • 199
  • 3
  • 6
2

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>
KBusc
  • 663
  • 8
  • 24
ukaliko
  • 172
  • 2
  • 7
2

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""

Tenusha Guruge
  • 2,147
  • 3
  • 18
  • 38
2

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(){}
Steephen
  • 14,645
  • 7
  • 40
  • 47
1

Same error i faced in eclipse version Oxygen.3a Release (4.7.3a) . There is issue in Maven Dependencies mismatch.To solve i have updated my Pom.xml with following dependecies.

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.netapp.junitnmactiopractice JunitAndMactioPractice 0.0.1-SNAPSHOT

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <junit.jupiter.version>5.1.1</junit.jupiter.version>
    <junit.platform.version>1.1.1</junit.platform.version>
</properties>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <version>${junit.platform.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

  • Have you seen the JUnit 5 User Guide regarding `junit-platform-runner`: "[_Runner for executing tests and test suites on the JUnit Platform in a JUnit 4 environment._](https://junit.org/junit5/docs/current/user-guide/#dependency-metadata)" – Gerold Broser Aug 11 '18 at 12:49
1

you should change

@Test
public static void testmethod(){}

to

@Test
public void testmethod(){}

the @Test is unsupport static method

Aegon
  • 21
  • 4
1

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.

1

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.

1

Try to reconfigure your unit test configurations to be the project JRE. enter image description here

Yigit Alparslan
  • 1,377
  • 1
  • 8
  • 13
1

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

Qalam
  • 49
  • 4
0

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.

Luke Hutchison
  • 8,186
  • 2
  • 45
  • 40
0

Just added my Test.class via eclipse menu and worked. Right click to project -> New -> Other -> JUnit -> JUnit Test Case

0

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.

0

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.

Ömer
  • 84
  • 8
0

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.

Arindam
  • 675
  • 8
  • 15
0

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

0

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

0

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.

0

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>
0

Removing the JUnit library, and then adding it again fixed this issue for me.

Tigris
  • 133
  • 8
0

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.

demongolem
  • 9,474
  • 36
  • 90
  • 105
0

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.

0

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.

bmck
  • 211
  • 2
  • 6
-1

Add in your POM:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>5.3.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Paul Roub
  • 36,322
  • 27
  • 84
  • 93