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

后端 未结 8 1842
抹茶落季
抹茶落季 2020-12-10 00:56

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


        
相关标签:
8条回答
  • 2020-12-10 01:16

    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!

    0 讨论(0)
  • 2020-12-10 01:22

    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

    0 讨论(0)
  • 2020-12-10 01:23

    adding following dependency helped in my case

     <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-commons</artifactId>
                <version>1.5.2</version>
     </dependency>
    
    0 讨论(0)
  • 2020-12-10 01:24

    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.

    0 讨论(0)
  • 2020-12-10 01:25

    As others suggested, using JUnit 5.5.2 version is the way to go here. There are different alternatives to achieve this:

    1. If you're using spring-boot-starter-parent, you can upgrade it to 2.2.0.RELEASE
    2. 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>
    3. 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)
    4. You can add the Junit BOM, using 5.5.2 version (see Prasanth Rajendran or Ramit answer)
    0 讨论(0)
  • 2020-12-10 01:31

    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.
    0 讨论(0)
提交回复
热议问题