score:23

Accepted answer

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

score:-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>

score: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.

score:0

just added my test.class via eclipse menu and worked. right click to project -> new -> other -> junit -> junit test case

score: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.

score: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.

score: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.

score: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

score: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

score: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.

score: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>

score:0

removing the junit library, and then adding it again fixed this issue for me.

score: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.

score: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.

score: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.

score: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>

score:1

you should change

@test
public static void testmethod(){}

to

@test
public void testmethod(){}

the @test is unsupport static method

score: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.

score: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.

score:1

try to reconfigure your unit test configurations to be the project jre. enter image description here

score: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

score: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.

score: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>

score: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>

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

score: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(){}

score: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).

score: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>

score: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>

score: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.

score: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>

score: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.

score: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.

score: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.

score:6

replace:

import org.junit.test;

with:

import org.junit.jupiter.api.test;

score: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

score: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.

score: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!

score:10

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

score: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.

score: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>

score: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

score:38

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

score:45

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

score:74

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.

score:169

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.


Related Query

More Query from same tag