java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException when trying run junit5 test with maven - maven

When trying to run tests using command mvn test I receive an error:
[ERROR] There was an error in the forked process
[ERROR] java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException
[ERROR] org.apache.maven.surefire.booter.SurefireBooterForkException: There was an error in the forked process
[ERROR] java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException
[ERROR] at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:656)
[ERROR] at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:282)
[ERROR] at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:245)
[ERROR] at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeProvider(AbstractSurefireMojo.java:1183)
[ERROR] at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:1011)
[ERROR] at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:857)
[ERROR] at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
[ERROR] at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
[ERROR] at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
[ERROR] at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
[ERROR] at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
[ERROR] at org.apache.maven.cli.MavenCli.execute(MavenCli.java:956)
[ERROR] at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
[ERROR] at org.apache.maven.cli.MavenCli.main(MavenCli.java:192)
[ERROR] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[ERROR] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR] at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
I run it under IntelliJ 2008.1, using maven 3.6.1 and surefire plugin in version 2.22.1
I have following dependencies in pom:
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
...
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
Recently I wasn't able to run any test cases but now after some configuration tries I receive this error.

Remove junit-platform-launcher, junit-jupiter-engine and junit-jupiter-api.
Add junit-jupiter. (junit-jupiter is aggregator)
Sources:
https://github.com/junit-team/junit5/issues/1773
It worked for me.

adding following dependency helped in my case
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.5.2</version>
</dependency>

As others suggested, using JUnit 5.5.2 version is the way to go here.
There are different alternatives to achieve this:
If you're using spring-boot-starter-parent, you can upgrade it to 2.2.0.RELEASE
If you use spring-boot-starter-parent (or spring-boot-dependencies), you can define a property to update just JUnit: <junit-jupiter.version>5.5.2</junit-jupiter.version>
If you're having this issue just in Eclipse, you can update it and add the JUnit 5 library to the Java Build Path (Project > Java Build Path > Libraries > Add Library > JUnit > JUnit 5 > Finish)
You can add the Junit BOM, using 5.5.2 version (see Prasanth Rajendran or Ramit answer)

I got the same problem with Gradle build and I got my issue resolved using the JUnit Bill of Materials(BOM), which will take care of Junit's direct and transitive dependencies version.
dependencyManagement {
imports {
mavenBom "org.junit:junit-bom:5.5.2"
}
}
dependencies {
...
testCompile('org.junit.jupiter:junit-jupiter-api')
testRuntime('org.junit.jupiter:junit-jupiter-engine')
testCompile('org.junit.jupiter:junit-jupiter-params')
testCompile('org.junit.platform:junit-platform-launcher')
testCompile('org.junit.platform:junit-platform-runner')
}
NOTE: I have not specified the version because the Junit BOM will take care of the Junit dependencies' version management role.

The following worked for me using surefire.plugin.version 2.22.2
junit-jupiter artifact brings in all other necessary artifacts.
<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>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<!--Optional: Supports running Junit4 along with Junit5 -->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

Few days ago I faced the similar issue. I tried all solutions suggested here, but none of them worked. In my case the error occurred because classpath ended up having two different versions of JUnit. I had spring-boot-test dependency wich uses JUnit 5.3.2. But then, I added JUnit 5.7.1. Therefore, my project was compiled with a newer version of JUnit (5.7.1) but found an older version (5.3.2) at runtime. As a result, the JUnit launcher tried to use a class that is not available in the older version of JUnit.In my case solution was to override the JUnit version managed by Spring like this:
<properties>
<junit-jupiter.version>5.7.1</junit-jupiter.version>
</properties>
Hope somebody find this helpful.

In my case the problem disappeared after deleting my local maven repository.
Don't know what library caused this strange behaviour, but now everything is working fine!

You can get the error like "java.lang.NoClassDefFoundError: org/junit/platform/commons/util/Preconditions" in case of tring to included some test class (from junit/assertj) in your production code.
Above is a consequence of
puttinh test library as a dependency NOT in scope test (so making it available at runtime)
possibly following on above error you importing such test utility class in your production code - src/main/java (for example you used import like import org.assertj.core.util.Lists in your production code from src/main/java.
the possibility is also that such test library was added as part of java9 module-info (like requires org.assertj.core)
NOTE: This offending dependency could be inside your module or any module included in dependencies.
For example your module has following dependency:
<dependency>
<groupId>com.acme</groupId>
<artifactId>your-another-module</artifactId>
</dependency>
and your another module includes spring-boot-starter-test in scope compile like:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>compile</scope> <!-- **or even scope is omitted and by default it is compile !** -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</exclusion>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
As mentioned inside above code snippet, the scope might have even been omitted which would default to "compile".
Now following on that error you might have a production class YourClass residing in your module and accidentally import some test code from test libraries like:
// here we have accidential import of Lists from assertj
import org.assertj.core.util.Lists;
class YourClass {
}
The solution is to:
fix the dependencies to make them in scope test and use correct imports in your production code
clean up module-info.java from any test libraries which are not needed in your production code

I have commented the below part in pom.xml:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
And I have added junit5 in java build path(project(right click->java build path->libraries->junit->junit5)
It worked for me. Mine is spring-boot project.

java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException when trying run junit5 test with maven
This error can be come due to the version mismatch of junit-plateform-engine
junit-jupiter-params and junit-platform-runner and also other respective dependencies.
So for the resolution of this issue you do not need to define version like 1.5.3 ...etc
you can define version as follows :
<dependencies>
<!--need to add for parameterized test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
**<version>${junit.jupiter.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.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
**<version>${junit.platform.version}</version>**
<scope>test</scope>
</dependency>
</dependencies>
So through this compatible version jar will be added in your project and project will run successfully

In case it helps anyone:
I was getting similar error along with "No tests found with test runner JUnit 5" when running JUnit 5 tests in Eclipse (gradle-based). Upgrading to JUnit 5.6.0 helped as per https://github.com/eclipse/buildship/issues/980

Try upgrade the Junit to 5.8.2 or greater, see Jupter Junit versions availables here:
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
The junit-platform-commons is transactive dependency from Junit, so if you upgrade Junit these transactive dependencies will also be updated.

Related

java.lang.ClassNotFoundException: io.netty.handler.logging.ByteBufFormat

I'm on integrating a third-party library into our application. For that I have added all the dependencies, however facing below error stack on application run.
Error stack-trace:
`
Caused by: java.lang.ClassNotFoundException: io.netty.handler.logging.ByteBufFormat
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 31 common frames omitted
`
Maven dependencies:
`
<dependency>
<groupId>com.affinda.api</groupId>
<artifactId>affinda-api-client</artifactId>
<version>0.4.2</version>
</dependency>
<dependency>
<groupId>com.microsoft.rest</groupId>
<artifactId>client-runtime</artifactId>
<version>1.7.14</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-client-runtime</artifactId>
<version>1.7.14</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-client-authentication</artifactId>
<version>1.7.14</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.84.Final</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
<version>4.1.84.Final</version>
</dependency>
`
I did check that ByteBufFormat is in netty-handler library from the docs and did check the dependency tree but haven't got any clue.
It might be too late, however I had the same problem.
The cause is that another dependency is overriding the netty version in your final pom. In my case it was spring boot dependencies (2.2.0-RELEASE) which was overriding netty to version 4.1.42Final instead of needed 4.1.86Final.
If you want to check who is responsible, you can run maven with goal help:effective-pom and search for the effective netty version management owner.
In order to solve the issue you can just specify the netty version among maven properties:
<properties>
.
.
<netty.version>4.1.86.Final</netty.version>
</properties>
In this way you will put back the expected netty version and everything will work like a charm.
Be careful to check that the other library is still working as well.

How do I include javax.servlet servlet-api 3.0.1 in my Maven project?

I'm using Maven 3.3. Do I need to add any special repositories to access the servlet-api 3.0.1 jar? I've added this to my pom.xml file
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
but I get this error when I try and do a build ...
[ERROR] Failed to execute goal on project core: Could not resolve dependencies for project org.collegeboard.springboard:core:jar:99.0.0-SNAPSHOT: Could not find artifact javax.servlet:servlet-api:jar:3.0.1 in thirdparty (https://nexus.getmyco.com/nexus/content/repositories/thirdparty/) -> [Help 1]
I need this dependency because I'm told taht anoterh dependency (spring-test-4.0.6.RELEASE) requires this.
Look at the pom of spring-test-4.0.6.RELEASE: http://search.maven.org/#artifactdetails|org.springframework|spring-test|4.0.6.RELEASE|jar
It actually depends on the following:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>

How to resolve Could not find artifact commons-math3:commons-math3:jar:3.4.1

I have a project that executes a JMeter tests suite though maven.
I use com.lazerycode.jmeter s jmeter-maven-plugin.
With version 2.0.3 my project runs well. but when I update teh plugins version to 2.1.0 the following error is thrown.
[ERROR] Failed to execute goal com.lazerycode.jmeter:jmeter-maven-plugin:2.1.0:configure (configure) on project my-regression: Could not find artifact commons-math3:commons-math3:jar:3.4.1 in central (https://repo.maven.apache.org/maven2) -> [Help 1]
I know by going through articles like this, that this can be resolved by excluding the dependencies.
but its just I don't know how to configure that dependency.
I tried the below.
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>commons-math3</groupId>
<artifactId>commons-math3</artifactId>
<version>3.4.1</version>
<exclusions>
<exclusion>
<groupId>commons-math3</groupId>
<artifactId>commons-math3</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
Which didn't work.
Will be glad to provide more info.
Use version 2.6.0 of the plugin and add under configuration node:
<excludedArtifacts>
<exclusion>commons-pool2:commons-pool2</exclusion>
<exclusion>commons-math3:commons-math3</exclusion>
</excludedArtifacts>

spring boot app initialization failure

Adding the spring boot dependencies in pom.xml and running spring boot app causes following errors -
> xception in thread "main" java.lang.IllegalAccessError: tried to
> access method
> org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(Ljava/lang/Class;Ljava/lang/ClassLoader;)Ljava/util/List;
> from class org.springframework.boot.SpringApplication at
> org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:392)
> at
> org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:384)
> at
> org.springframework.boot.SpringApplication.initialize(SpringApplication.java:260)
> at
> org.springframework.boot.SpringApplication.<init>(SpringApplication.java:236)
> at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)
> at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)
> at com.catalina.platform.batch.Application.main(Application.java:12)
See below the snap shot of my pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.3.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-ftp</artifactId>
<version>${spring.ftp.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Usually IllegalAccessError problems are due to an incorrect version of Spring being pulled in by Maven. Spring Boot 1.3.6 needs Spring Framework 4.2.7.
The spring-boot-dependencies dependencyManagement import is normally enough to ensure the correct version is used and the limited section of the POM that you've posted looks correct.
I would suggest running mvn dependency:tree to check that the correct version is being pulled in. Also check that you're on the latest Maven release.
Failing that, a project that reproduces the issue would help a lot with tracking down the cause.
I too had the same problem when i created a Spring Simple Web Maven Project and added few other dependencies in my pom.xml to write a web service for spring boot application
Updating my dependencies so that the version mismatch is handled and then doing Run As-->Maven clean , Run As --> Maven Install followed by Maven--Update Project worked in my case

maven artifactId hadoop 2.2.0 for hadoop-core

I am migrating my application from hadoop 1.0.3 to hadoop 2.2.0 and maven build had hadoop-core marked as dependency. Since hadoop-core is not present for hadoop 2.2.0. I tried replacing it with hadoop-client and hadoop-common but I am still getting this error for ant.filter. Can anybody please suggest which artifact to use?
previous config :
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.0.3</version>
</dependency>
New Config:
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.2.0</version>
</dependency>
Error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project event: Compilation failure: Compilation failure:
[ERROR] /opt/teamcity/buildAgent/work/c670ebea1992ec2f/event/src/main/java/com/intel/event/EventContext.java:[27,36] package org.apache.tools.ant.filters does not exist
[ERROR] /opt/teamcity/buildAgent/work/c670ebea1992ec2f/event/src/main/java/com/intel/event/EventContext.java:[27,36] package org.apache.tools.ant.filters does not exist
[ERROR] /opt/teamcity/buildAgent/work/c670ebea1992ec2f/event/src/main/java/com/intel/event/EventContext.java:[180,59] cannot find symbol
[ERROR] symbol: class StringInputStream
[ERROR] location: class com.intel.event.EventContext
We mainly depend on hdfs api for our application. When we migrated to hadoop 2.X, we were surprised to see the changes in dependencies. We started adding dependencies one at a time. Today we depend on the following core libraries.
hadoop-annotations-2.2.0
hadoop-auth-2.2.0
hadoop-common-2.2.0
hadoop-hdfs-2.2.0
hadoop-mapreduce-client-core-2.2.0
In addition to these we depend on test libraries too. Based on your needs, you may want to include hadoop-hdfs and hadoop-mapreduce-client to the dependencies along with hadoop-common.
Try with these artifacts, word fine on my sample project wordcount
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
</dependency>
Maven dependencies can be got from this link.
As far as hadoop-core dependies goes, hadoop-core was the name for hadoop 1.X and just renaming the version to 2.X wont help. Also in a hadoop 2.X project using the hadoop 1.X dependency gives an error like
Caused by: org.apache.hadoop.ipc.RemoteException: Server IPC version 9 cannot communicate with client version 4
Thus it is suggested not to use it. I have been using the following dependencies in my hadoop
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-jobclient</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-common</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
You can try these.

Resources