NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass - java

I have the test that leads to error. I tried to execute it in the IntelliJ Idea 2018.3.2. All jupiter and junit dependencies have version RELEASE
The full text of error:
Dec 26, 2018 1:17:17 AM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to execute tests
java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(Ljava/lang/String;)Lorg/junit/platform/commons/function/Try;
at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.createAbortedExecutionPredicate(OpenTest4JAndJUnit4AwareThrowableCollector.java:40)
at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.<clinit>(OpenTest4JAndJUnit4AwareThrowableCollector.java:30)
at org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory.createThrowableCollector(JupiterThrowableCollectorFactory.java:34)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:68)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
the test has the following view
import biz.Services.msg.BookTimeMsgService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertTrue;
#ExtendWith(MockitoExtension.class)
public class MsgReceiverTest {
#Mock
BookTimeMsgService bookTimeMsgService;
#InjectMocks
MsgReceiver msgReceiver;
#Test
public void msgReceiverTest_bookTimeServiceShouldObtainMsg() {
assertTrue(true);
}
part of my pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>RELEASE</version>
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
How to fix the issue?

I changed the version of jupiter and junit to 5.3.2 and the problem has gone

I was able to run the Junit 5 tests after adding the platform and launcher dependency:
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-commons -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>

minimal required pom.xml setup for spring-boot 2.1.3 and junit 5.4.0 testing next:
<?xml version="1.0" encoding="UTF-8"?>
<project ...>
<parent>
<groupId>org.springframework.boot</g..roupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<junit-jupiter.version>5.4.0</junit-jupiter.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

use:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
instead
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
you can find more details here

Combos of Maven/Gradle, Eclipse/IntelliJ using Spring Boot/or not while including Junit4/Junit5 throw different flavors of this NoSuchMethodError. This leads to many different solutions all of which only fix a portion of people's problems. This long winded answer is an attempt to explain the root cause and steps you can take to fix your problem whichever flavor you are using.
Root cause:
At compile time, one version of ReflectionUtils (I've seen other classes here too) was used, and at runtime, a different one was used. The runtime version was expecting a certain method to exist, but either it didn't exist, or it existed with a different signature (number/type of parameters)
Note, when I say compile time, this can mean (and usually does) mean the version of ReflectionUtils that was used to compile a third party jar inside your classpath.
The solution will always be to change or exclude some version of some jar. The trick is to find which jar and which version.
To solve:
First, identify what version of ReflectionUtils (or other class it is complaining about) is being used at runtime. This is fairly easy. In eclipse, do an Open Type, in IntelliJ, use a navigate/go to class. Make sure to fully qualify the path. If you have exactly 1 version on the path, this is good. If you have more than on version on the path, your first action to take is to modify your build path to exclude one of the versions.
If you cannot modify the build path, even temporarily, it is harder to identify what version of ReflectionUtils is being used at runtime. Since JUnit 4 and 5 begins and dies before you can run any of your code, you cannot insert a
CodeSource src = ReflectionUtils.class.getProtectionDomain().getCodeSource();
if (src != null) {
URL jar = src.getLocation();
System.out.println(jar.toString());
}
to get the location of the runtime jar. But you can try:
1 - create a fresh new project, a fresh pom, put 1 junit inside of it, and run it with the above code and see what comes out. This may work because whatever conflict was in the non working project has been removed. This may not work in which case move to #2. This also may give a false result because the runtime path of a fresh project may be different than that of the non working project. Try updating the pom of the fresh project to look more and more like that of the non working project. Remember, the goal is to find what version of the class is being used at runtime
2 - Use google. Yah I know you got here by using google but this time change your search to see if someone documented "This version of JUnit5 uses this version of ReflectionUtils at runtime" or "This version of IntelliJ puts this version of ReflectionUtils on the classpath" or "This version of Spring Boot is putting this version of ReflectionUtils on the classpath". Remember, the goal is to find what version of the class is being used at runtime
3 - Hijack a class (advanced). When the stacktrace comes out, you will see several classes in the stacktrace. Try to find the source code for the class on the internet and create that package and class in your project. Then insert the code above and try to run the test which was failing.
Once you have found which version of ReflectionUtils is used at runtime, you need to find out what version was used at compile time
Identify which jar ReflectionUtils lives inside.
In the stacktrace which pops out when you run your test, identify the top class and method which died trying to use ReflectionUtils. This is the code which was compiled with an incompatible version of ReflectionUtils. Find out what jar that class is inside. I'll call this jar the conflicting jar. Try and get a dependency tree for that conflicting jar. The dependency tree should indicate which version of the ReflectionUtils jar the conflicting jar was dependent on.
Once you have found which version of ReflectionUtils jar was used at compile time and runtime, you likely will see that they are different and also incompatible. At this point, you will have to
1 - change the version of the ReflectionUtils jar used at runtime to match the version of ReflectionUtils needed by the conflicting jar
2 - change the version of the conflicting jar to one which is dependent on the same version of the ReflectionUtils jar which is on the runtime path
3 - if the conflicting jar is a dependency of some parent jar in build path and not explicity included by your build path, try excluding the conflicting jar from the parent jar and add a newer version explicitly. This may solve the problem - but this is, of course, the type of behavior which created this mess in the first place.
Other actions you can take:
If you know that you are using both JUnit4 jars as well as JUnit5 in your project, try removing one or the other completely from the compile time and runtime paths. The Assert patterns of the two paradigms are different enough that you likely do not want to mix them in the same project as it could lead to confusion of which parameters are the expected results, the message, and the variable under test.

Related

JUnit5 comparing two ArrayLists results in No tests Found with test runner [duplicate]

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:
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.
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:
I ran the test again and it worked.
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.
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
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
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:
See also my video that shows Java 9 and JUnit 5 in Eclipse Oxygen.1a in action
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.
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
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>
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.
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
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!
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.
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
From the start the error message tell you that class is not found : NoClassDefFoundError that mean the PATH to junit is the problem.
Press right click to project folder and choose Properties OR select project folder and press combination cmd + i.
select from list "Java Build Path".
select "Libraries" tab
If JUnit 5(or JUnit 4) is added to "Modulepath", select the "JUnit 5" and press Remove.
select "Classpath", press "Add Library...".
from opened "Add Library" window, select JUnit, press next.
Select JUnit library version that you need and press Finish.
That is all. Try to run test again.
replace:
import org.junit.Test;
with:
import org.junit.jupiter.api.Test;
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.
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>
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.
#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.
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).
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>
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>
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.
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>
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>
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""
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(){}
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>
you should change
#Test
public static void testmethod(){}
to
#Test
public void testmethod(){}
the #Test is unsupport static method
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.
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.

Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory

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:
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.
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:
I ran the test again and it worked.
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.
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
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
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:
See also my video that shows Java 9 and JUnit 5 in Eclipse Oxygen.1a in action
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.
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
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>
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.
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
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!
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.
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
From the start the error message tell you that class is not found : NoClassDefFoundError that mean the PATH to junit is the problem.
Press right click to project folder and choose Properties OR select project folder and press combination cmd + i.
select from list "Java Build Path".
select "Libraries" tab
If JUnit 5(or JUnit 4) is added to "Modulepath", select the "JUnit 5" and press Remove.
select "Classpath", press "Add Library...".
from opened "Add Library" window, select JUnit, press next.
Select JUnit library version that you need and press Finish.
That is all. Try to run test again.
replace:
import org.junit.Test;
with:
import org.junit.jupiter.api.Test;
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.
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>
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.
#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.
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).
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>
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>
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.
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>
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>
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""
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(){}
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>
you should change
#Test
public static void testmethod(){}
to
#Test
public void testmethod(){}
the #Test is unsupport static method
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.
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.

Code in IntelliJ IDEA marked with compiler error where it works fine in Eclipse

I'm trying to setup my IntelliJ workspace to do development on an eclipse project. One of the things I've run into is rather confusing:
Error:(24, 8) java: SomeClass.java:24: getHeader(java.lang.String) in org.springframework.mock.web.MockHttpServletResponse cannot implement getHeader(java.lang.String) in javax.servlet.http.HttpServletResponse; attempting to use incompatible return type
found : java.lang.Object
required: java.lang.String
The problem is the following class definition:
public class SomeClass extends MockHttpServletResponse {
The problem seems to be because MockHttpServletResponse implements Collection<String> getHeaders(String) as public List getHeaders(String name). Here, I can see that the implementing method uses a raw List where the parent asks for a generic Collection typed with String. Aside from being potentially not type-safe, why would IntelliJ mark this as a complier error instead of a warning?
I have no option of changing any of these libraries. I'm simply trying to make work in IntellJ 14 what already works without complaints in Eclipse 4.3+.
EDIT:
I have since updated to IntelliJ 15.0, and the project is using Java 1.7 now instead of 1.6. I am still running into this issue with IntelliJ, but the issue is not presenting itself at all in Eclipse. I can compile the project using existing Ant scripts via IntelliJ, but I cannot debug through the IDE.
Here is my class definition
public class ExecutableServletResponse extends MockHttpServletResponse {
...
Here is the error showing in my 'Messages' pane:
Error:(24, 8) java: getHeader(java.lang.String) in org.springframework.mock.web.MockHttpServletResponse cannot implement getHeader(java.lang.String) in javax.servlet.http.HttpServletResponse
return type java.lang.Object is not compatible with java.lang.String
The project SDK is using version 1.7 (1.7.0_79 to be exact). Language level is 7. Module SDK and Language Levels match the project.
I've tried using the eclipse compiler, but the app still doesn't fully compile, and will fail to run presumably because it fails to compile this class, and a whole part of the webapp doesn't compile as a result.
Here's a screenshot of my error, FWIW:
You are seeing the error in your class, but the real issue is that the Spring mock library is not compatible with the Servlet Specification you are using. This may happen if you upgraded to the Servlet 3.0 Spec (or added a dependency that pulled it in transitively). Check your dependencies and ensure that either:
Only Servlet 2.5 is provided, or
You are using a version of Spring that is compatible with Servlet 3.0. Also ensure that all of your Spring dependencies are using the same version.
This combination should work:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
</dependency>
as should this:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.0.RELEASE</version>
<scope>test</scope>
</dependency>
But this will fail:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.2.RELEASE</version>
<scope>test</scope>
</dependency>
The fact that it works in Eclipse but not in IntelliJ suggests that you have multiple dependencies that provide the same classes. There is no guarantee as to which jar the system will use to load the class. This may either be because you have both servlet-api and javaee-web-api on your classpath or because you have both spring-mock and spring-test on your classpath. After version 2.0.8, the classes in spring-mock were moved to spring-test and only version 3.1.0.RELEASE and higher of spring-test are compatible with Servlet 3.0.

Jena NoClassDefFoundError with Maven

I have a Java Maven project that I developed a while ago and that doesn't work anymore. It uses a parent pom together with another Maven project in which I think the Jena version was changed and it also uses an external library that uses Jena. The Maven dependency is:
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>jena</artifactId>
<version>2.6.4</version>
</dependency>
When I execute my tests I get the following errors:
java.lang.NoClassDefFoundError: Could not initialize class
com.hp.hpl.jena.query.ARQ
java.lang.NoClassDefFoundError: org/apache/jena/iri/IRIFactory
at org.openjena.riot.system.PrefixMap.add(PrefixMap.java:54)
at com.hp.hpl.jena.sparql.util.MappingRegistry.addPrefixMapping(MappingRegistry.java:33)
at com.hp.hpl.jena.query.ARQ.init(ARQ.java:449) [...]
The errors are not thrown by my code directly but by the library I include. Can I prevent this by downgrading the Jena version in the parent pom or what can I do here?
P.S.: I now have a minimal code example that reproduces the error (java.lang.NoClassDefFoundError: org/apache/jena/iri/IRIFactory):
import org.junit.Test;
import com.hp.hpl.jena.query.ARQ;
public class DependencyTest
{
#Test
public void testARQ()
{
ARQ a = new ARQ();
}
}
And I guess it comes from this dependency:
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.9.1-incubating-SNAPSHOT</version>
</dependency>
I know there is probably a factory instead of a constructor but I guess this still shows where the problem is.
P.S.: I noticed that I had the dependencies "jena", "arq" and "jena-arq":
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>arq</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.9.1-incubating-SNAPSHOT</version>
</dependency>
dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>jena</artifactId>
<version>2.6.4</version>
</dependency>
So I thought maybe I have too much overlapping dependencies and commented out "jena" and "arq". But I still get the error
java.lang.NoClassDefFoundError: Could not initialize class com.hp.hpl.jena.query.ARQ
at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.<init> [...]
I also tried out forcing a non-snapshot-version 2.9.0-incubating, but then I still get the NoClassDefFoundError with and without using the "jena" and "arq"-dependencies.
P.P.S.:
I still get the same error even when I use the following dependencies:
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.9.0-incubating</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>2.7.0-incubating</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-iri</artifactId>
<version>2.7.0-incubating</version>
</dependency>
You can search for the missing class using the Maven Central search application
http://search.maven.org/#search|ga|1|fc%3A%22com.hp.hpl.jena.query.ARQ%22
It demonstrates that the dependency you appear to be missing is:
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>arq</artifactId>
<version>2.6.0</version>
</dependency>
Doesn't appear to be a version 2.6.4, but you're probabily best advised to go for a more modern version (This project was recently donated to apache)
Instructions for using Apache Jena with Maven are here:
incubator.apache.org/jena/download/maven.html
Specifying ARQ 2.9.0 as a dependency in your project pom.xml will pull in the other Jena components that you need.
I finally resolved this error by excluding the "jena"-Dependency brought in as a transitive dependency from some library.

Eclipse compilation error: The hierarchy of the type 'Class name' is inconsistent

I have downloaded some open source software written in Java and tried to compile it using Eclipse.
I got the error: "The hierarchy of the type 'Class name' is inconsistent" in some files.
What causes these errors and how do I fix them?
It means you are trying to implement a non-existing interface or you're extending an non-existing class.
Try to refresh your Eclipse.
If it doesn't work, it may mean that you have a reference to a JAR that is not in the build path.
Check your project's classpath and verify that the jar containing the interface or the class is in it.
Sometimes it happens when you add a jar that YOU need, but don't include the jars that IT needs. In my case adding all the jars in tomcat/lib helped me to solve this problem. I am working on a web-app.
Check your errors (tab "markers"). I had also the following error:
Archive for required library in project cannot be read...
and when that was fixed the "inconsistent-error" disappeared.
Actually I had added jars to the build path, but for some reason they could not be read with error
Archive for required library in project cannot be read or is not a valid ZIP file
So instead I added them as "External Jars". That helped and all compilation problems were no more!
I had this problem after I upgraded the JDK to a new version. I had to update the references to libraries in Project Properties/Java Build Path.
One more case I have had. Give the correct project path, and import it to eclipse.
Then go to Project--> Clean --> Clean all projects.
You should clean the project , or restart Eclipse.
You will see this error in case a some class in your library file you have in classpath has reference to non-existing classe(s) which could be in another jar file. Here, I received this error when I did not add org.springframework.beans-3.1.2.RELEASE.jar and had extended a class from org.springframework.jdbc.core.support.JdbcDaoSupport, which was in org.springframework.jdbc-3.1.2.RELEASE.jar of my classpath.
The problem may be that you have included incorrect jars. I had the same problem and the reason was that I had included incorrect default JRE library in the build path of the project. I had installed Java with another version and was including JRE files of Java with a different version. (I had installed JRE 1.6 in my system and was having JRE library 1.7 included in the build path due to previously installed Java) May be you can check if the JRE library that you have included in the build path is of correct version ie. of Java version that you have installed in your system.
I 've experienced this problem on Eclipse Juno, the root cause was that although some spring jars were being included by transient maven dependencies they were included in incorrect versions.
So you should check if using a modularized framework as spring that every module (or at least the most important: core, beans, context, aop, tx, etc.) are in the same version.
To solve the problem I 've used maven dependnecy exclusions to avoid incorrect version of transient dependencies.
Error : the hierarchy of the type "class name" is inconsistent error.
solution :
class OtherDepJar {} --> is inside "other.dep.jar" .
class DepJar extends OtherDepJar {} --> is inside "dep.jar".
class ProblematicClass extends DepJar {} --> is inside current project .
If dep.jar is in the project's classpath, but other.dep.jar isn't in the project's classpath, Eclipse will show the" The hierarchy of the type ... is inconsistent error"
To me, the issue was due to wrong imports. In fact, one need to update the imports after adding the v7 support library.
It can be fixed by doing as follows, for each class of your project:
Delete all the lines with import android.[*], in each class
Reorganize your imports: from the context menu select Source/Organize Imports or (CTRL+SHIFT+O)
When prompted, select the libraries android.support.[*] (and not android.[*]).
It was definitely because missing dependencies that were not in my maven pom.xml.
For example, I wanted to create integration tests for my implementation of the broadleaf ecommerce demo site.
I had included a broadleaf jar with integration tests from broadleaf commerce in order to reuse their configuration files and base testing classes. That project had other testing dependencies that I had not included and I received the "inconsistent hierarchy" error.
After copying the "test dependencies" from broadleaf/pom.xml and the associated properties variables that provided the versions for each dependency in broadleaf/pom.xml, the error went away.
The properties were:
<geb.version>0.9.3</geb.version>
<spock.version>0.7-groovy-2.0</spock.version>
<selenium.version>2.42.2</selenium.version>
<groovy.version>2.1.8</groovy.version>
The dependencies were:
<dependency>
<groupId>org.broadleafcommerce</groupId>
<artifactId>integration</artifactId>
<type>jar</type>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.broadleafcommerce</groupId>
<artifactId>broadleaf-framework</artifactId>
<version>${blc.version}</version><!--$NO-MVN-MAN-VER$ -->
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>1.3</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>2.5.1</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymockclassextension</artifactId>
<version>2.4</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.9</version>
<type>jar</type>
<classifier>jdk15</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gebish</groupId>
<artifactId>geb-core</artifactId>
<version>${geb.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gebish</groupId>
<artifactId>geb-spock</artifactId>
<version>${geb.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>${spock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>${selenium.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>${selenium.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>${selenium.version}</version>
<scope>test</scope>
</dependency>
<!-- Logging -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.6.1</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.1</version>
<type>jar</type>
<scope>test</scope>
</dependency>
If the extended class has the issue then the above error message will gets displayed.
Example
class Example extends Example1 {
}
fix the issues in Example1
I had the same exact problem marker and solved it by removing the #Override annotation from a method that was in fact the first implementation (the "super" one being an abstract method) and not an override.
In my case, the import references in many of the classes contained an extra word. I solved it by editing all the files to have the correct imports. I started doing the edits manually. But when I saw the pattern, I automated it with a find..replace in eclipse. This resolved the error.
For me it was changing the Android API level to one with Google APIs
I was having this problem too... I found out that the hierarchy of the class that was throwing this exception, cannot be traced all way back to its root class by eclipse... I Explain:
In my case, I have 3 java project: A, B and C... where A and B are maven projects and C a regular java eclipse project...
In the project A, i have the interface "interfaceA" ...
In the project B, i have the interface "interfaceB" that extends "interfaceA"
In the project C, i have the concrete class "classC" that implements "interfaceB"
The "project C" was including the "project B" in its build path but not "project A" (so that was the cause of the error).... After including "project A" inside the build path of "C", everything went back to normal...
I had a class that extends LabelProvider in a project with OSGi, there the error occured. The solution was: Adding org.eclipse.jface to the required plugins in the manifest.mf instead of importing the single packages like org.eclipse.jface.viewers
if you are importing the eclipse project just
1. Go to the java build path setting under the project properties.
2. In case the JRE System library has an error sign attach to it double click it to open the Edit library window
3. Change the execution environment to the correct java version of the system or choose edit the other settings by checking the radio buttons assign to them.
4. Click finish
When importing a GWT project in Eclipse without installing "Google Plugin for Eclipse", this will occur. After installing "Google Plugin for Eclipse", this error will disappear.
Right click on the project folder and select "Java Build Path". Under "Java Build Path" you should be able to see libraries. Eclipse will show errors in any of those libraries. Fixing those issue will help to resolve the issue.
I had this error after doing some git merge from a branch where my classes extended a new interface.
It was enough to Refresh (F5) the File-Tree in the Package Explorer frame of Eclipse.
It seems that Eclipse did not update everything properly and so the classes were extending a non-existing-yet interface. After refresh, all errors disappeared.
I had to switch from Eclipse Oxygen that I got from IBM and used IBM JDK 8 to Eclipse Photon and Oracle JDK 8. I'm working on Java customizations for maximo.
This error will also appear when one of the jars required by the existing dependencies is not available in current project path.
Ex:-> Current Proj depends on Lib1 depends on Lib2.
If we use one of the classes in Lib1 but Lib2 is not packaged in Lib1 or not available in current project path you'll see the issue.

Categories