web-dev-qa-db-fra.com

java.lang.NoClassDefFoundError: org / junit / platform / commons / PreconditionViolationException lors de la tentative d'exécution du test junit5 avec maven

Lorsque j'essaie d'exécuter des tests à l'aide de la commande test mvn Je reçois une erreur:

[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)

Je l'exécute sous IntelliJ 2008.1, en utilisant maven 3.6.1 et le plugin surefire dans la version 2.22.1

J'ai les dépendances suivantes dans 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>

Récemment, je n'ai pas pu exécuter de cas de test, mais maintenant, après quelques tentatives de configuration, je reçois cette erreur.

17
kris82pl

Retirer junit-platform-launcher, junit-jupiter-engine et junit-jupiter-api.

Ajouter junit-jupiter. (junit-jupiter est agrégateur)

Sources:

14
IndustryUser1942

l'ajout de la dépendance suivante a aidé dans mon cas

 <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-commons</artifactId>
            <version>1.5.2</version>
 </dependency>
11
Dmitry Artyukhov

Comme d'autres l'ont suggéré, utiliser la version de JUnit 5.5.2 Est la voie à suivre ici. Il existe différentes alternatives pour y parvenir:

  1. Si vous utilisez spring-boot-starter-parent, Vous pouvez le mettre à niveau vers 2.2.0.RELEASE
  2. Si vous utilisez spring-boot-starter-parent (Ou spring-boot-dependencies), Vous pouvez définir une propriété pour mettre à jour uniquement JUnit: <junit-jupiter.version>5.5.2</junit-jupiter.version>
  3. Si vous rencontrez ce problème uniquement dans Eclipse, vous pouvez le mettre à jour et ajouter la bibliothèque JUnit 5 au chemin de génération Java (Projet> Java Chemin de génération> Bibliothèques> Ajouter une bibliothèque> JUnit> JUnit 5> Terminer)
  4. Vous pouvez ajouter la nomenclature Junit, en utilisant la version 5.5.2 (voir Prasanth Rajendran ou Ramit réponse)
11
Gerardo Roza

Ce qui suit a fonctionné pour moi en utilisant surefire.plugin.version 2.22.2

l'artefact junit-jupiter apporte tous les autres artefacts nécessaires.

<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>
6
Ramit

J'ai eu le même problème avec Gradle build Et j'ai résolu mon problème en utilisant JUnit Bill of Materials(BOM), qui prendra en charge la version des dépendances directes et transitives de Junit.

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 : Je n'ai pas spécifié la version car la nomenclature Junit prendra en charge le rôle de gestion de version des dépendances Junit.

6
Prasanth Rajendran

Java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException lors de la tentative d'exécution du test junit5 avec maven

Cette erreur peut être due à la non-concordance de version de junit-plate-forme-moteur junit-jupiter-params et junit-platform-runner et également d'autres dépendances respectives.

Donc, pour la résolution de ce problème, vous n'avez pas besoin de définir une version comme 1.5.3 ... etc

vous pouvez définir la version comme suit:

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

Donc, grâce à cette version compatible, le pot sera ajouté dans votre projet et le projet se déroulera avec succès

0
SumitTyagi

Dans mon cas, le problème a disparu après suppression de mon référentiel maven local. Je ne sais pas quelle bibliothèque a provoqué ce comportement étrange, mais maintenant tout fonctionne bien!

0
tangens

J'ai commenté la partie ci-dessous dans 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>

Et j'ai ajouté junit5 dans Java chemin de construction (projet (clic droit-> Java build path-> bibliothèques-> junit-> junit5)). Cela a fonctionné pour moi. Le mien est un projet à démarrage par ressort.

0
Boney