java.lang.NoClassDefFoundError: io/restassured/path/xml/mapper/factory/JAXBObjectMapperFactory

java.lang.NoClassDefFoundError: io/restassured/path/xml/mapper/factory/JAXBObjectMapperFactory

我正在尝试 运行 一个 Cucumber 测试用例场景,但在 IntelliJ-IDEA 中编译后收到错误。我已经在此处发布了 build.gradle 文件。我不确定为什么我一 运行 程序就收到这个错误。有人可以启发我如何解决这个问题吗?我已经收到这个错误一天多了,但仍然没有解决方案。

错误 StackTrace

java.lang.NoClassDefFoundError: io/restassured/path/xml/mapper/factory/JAXBObjectMapperFactory
    at io.restassured.config.RestAssuredConfig.<init>(RestAssuredConfig.java:41)
    at io.restassured.RestAssured.<clinit>(RestAssured.java:420)
    at AbtMainTestControl.PostJsonDetail.postJsonPayload(PostJsonDetail.java:17)
    at AbtMainTestControl.AbtCardPavTest.first_PAV_is_Transact(AbtCardPavTest.java:110)

Caused by: java.lang.ClassNotFoundException: io.restassured.path.xml.mapper.factory.JAXBObjectMapperFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    at io.restassured.config.RestAssuredConfig.<init>(RestAssuredConfig.java:41)
    at io.restassured.RestAssured.<clinit>(RestAssured.java:420)
    at AbtMainTestControl.PostJsonDetail.postJsonPayload(PostJsonDetail.java:17)
    at AbtMainTestControl.AbtCardPavTest.first_PAV_is_Transact(AbtCardPavTest.java:110)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at cucumber.runtime.Utils.call(Utils.java:26)
    at cucumber.runtime.Timeout.timeout(Timeout.java:16)
    at cucumber.runtime.Utils.invoke(Utils.java:20)
    at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:57)
    at cucumber.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:50)
    at cucumber.runner.TestStep.executeStep(TestStep.java:65)
    at cucumber.runner.TestStep.run(TestStep.java:50)
    at cucumber.runner.PickleStepTestStep.run(PickleStepTestStep.java:43)
    at cucumber.runner.TestCase.run(TestCase.java:46)
    at cucumber.runner.Runner.runPickle(Runner.java:50)
    at cucumber.runtime.Runtime.run(Runtime.java:104)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at cucumber.runtime.Runtime$SameThreadExecutorService.execute(Runtime.java:258)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
    at cucumber.runtime.Runtime.run(Runtime.java:101)
    at io.cucumber.core.cli.Main.run(Main.java:43)
    at io.cucumber.core.cli.Main.main(Main.java:14)


java.lang.NoClassDefFoundError: Could not initialize class io.restassured.RestAssured
    at AbtMainTestControl.PostJsonDetail.postJsonPayload(PostJsonDetail.java:17)
    at AbtMainTestControl.AbtCardPavTest.first_PAV_is_Transact(AbtCardPavTest.java:110)
    at ✽.First PAV is 

java.lang.NoClassDefFoundError: io/restassured/path/xml/mapper/factory/JAXBObjectMapperFactory
    at io.restassured.config.RestAssuredConfig.<init>(RestAssuredConfig.java:41)
    at io.restassured.RestAssured.<clinit>(RestAssured.java:420)
    at AbtMainTestControl.PostJsonDetail.postJsonPayload(PostJsonDetail.java:17)
    at AbtMainTestControl.AbtCardPavTest.first_PAV_is_Transact(AbtCardPavTest.java:110)
    at ✽.First PAV is Transact(file:/C:/Users/lukegoh/Documents/GitHub/AbtMainTestControl/AbtMainTestControl/src/test/resources/features/AbtCardPAVTest.feature:10)
Caused by: java.lang.ClassNotFoundException: io.restassured.path.xml.mapper.factory.JAXBObjectMapperFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    at io.restassured.config.RestAssuredConfig.<init>(RestAssuredConfig.java:41)
    at io.restassured.RestAssured.<clinit>(RestAssured.java:420)
    at AbtMainTestControl.PostJsonDetail.postJsonPayload(PostJsonDetail.java:17)
    at AbtMainTestControl.AbtCardPavTest.first_PAV_is_Transact(AbtCardPavTest.java:110)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at cucumber.runtime.Utils.call(Utils.java:26)
    at cucumber.runtime.Timeout.timeout(Timeout.java:16)
    at cucumber.runtime.Utils.invoke(Utils.java:20)
    at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:57)
    at cucumber.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:50)
    at cucumber.runner.TestStep.executeStep(TestStep.java:65)
    at cucumber.runner.TestStep.run(TestStep.java:50)
    at cucumber.runner.PickleStepTestStep.run(PickleStepTestStep.java:43)
    at cucumber.runner.TestCase.run(TestCase.java:46)
    at cucumber.runner.Runner.runPickle(Runner.java:50)
    at cucumber.runtime.Runtime.run(Runtime.java:104)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at cucumber.runtime.Runtime$SameThreadExecutorService.execute(Runtime.java:258)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
    at cucumber.runtime.Runtime.run(Runtime.java:101)
    at io.cucumber.core.cli.Main.run(Main.java:43)
    at io.cucumber.core.cli.Main.main(Main.java:14)

java.lang.NoClassDefFoundError: Could not initialize class io.restassured.RestAssured
    at AbtMainTestControl.PostJsonDetail.postJsonPayload(PostJsonDetail.java:17)
    at AbtMainTestControl.AbtCardPavTest.first_PAV_is_Transact(AbtCardPavTest.java:110)
    at ✽.First PAV is Transact(file:/C:/Users/lukegoh/Documents/GitHub/AbtMainTestControl/AbtMainTestControl/src/test/resources/features/AbtCardPAVTest.feature:10)


Process finished with exit code 1

build.gradle

plugins {
    id 'org.springframework.boot' version '2.3.4.RELEASE'
}

apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven-publish'


sourceCompatibility = 1.8
targetCompatibility = 1.8


group 'AbtMainTestControl'
version '1.0-SNAPSHOT'

// Versioning of dependencies
wrapper.gradleVersion = '6.6.1'
def cucumberVersion = '4.7.1'
def junitVersion = '5.5.0'
def restVersion = '4.1.2'
def apacheDrillVersion = '1.17.0'



repositories {
    jcenter()
    mavenCentral()
}


dependencies {
    compile group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: '1.9.13'
    compile group: 'com.opencsv', name: 'opencsv', version: '4.0'

    compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.12'

    compile group: 'org.apache.drill.tools', name: 'tools-parent', version: "${apacheDrillVersion}", ext: 'pom'

    compile group: 'de.monochromata.cucumber', name: 'reporting-plugin', version: '3.0.9'
    
    testCompile group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '6.1.0.jre8'

    compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.7.0'

    compile 'org.modelmapper:modelmapper:2.3.3'

    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('org.springframework.boot:spring-boot-starter-data-jpa')
    
    testImplementation "io.cucumber:cucumber-java:${cucumberVersion}"
    testImplementation "io.cucumber:cucumber-junit:${cucumberVersion}"
    testImplementation "io.rest-assured:rest-assured:${restVersion}"
    testImplementation "io.rest-assured:json-path:${restVersion}"
    testImplementation "io.rest-assured:json-schema-validator:${restVersion}"

    testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
    testRuntimeOnly "org.junit.vintage:junit-vintage-engine:${junitVersion}"
    implementation 'junit:junit:4.12'

    compileOnly 'org.projectlombok:lombok:1.18.12'
    annotationProcessor 'org.projectlombok:lombok:1.18.12'

    testCompileOnly 'org.projectlombok:lombok:1.18.12'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'

    implementation 'org.mapstruct:mapstruct:1.4.1.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.1.Final'

    compile group: 'org.hibernate', name: 'hibernate-gradle-plugin', version: '5.4.22.Final'
}


configurations {
    cucumberRuntime {
        extendsFrom testImplementation
    }
}

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath =  configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin',
                    'pretty',
                    '--glue',
                    'gradle.cucumber',
                    'src/test/resources/features',

            ]
        }
    }
}


test {
    //useJUnitPlatform()
    systemProperty "cucumber.options", System.properties.getProperty("cucumber.options")
}

注意:我已经删除了错误的冗余部分。

我通过在 build.gradle 文件中明确声明 json-pathxml-path 设法解决了 java.lang.NoClassDefFoundError: io/restassured/path/xml/mapper/factory/JAXBObjectMapperFactory 错误。

这是我声明的方式。

build.gradle

plugins {
    id 'org.springframework.boot' version '2.3.4.RELEASE'
}

apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven-publish'


sourceCompatibility = 1.8
targetCompatibility = 1.8


group 'AbtMainTestControl'
version '1.0-SNAPSHOT'

// Versioning of dependencies
wrapper.gradleVersion = '6.6.1'
def cucumberVersion = '4.7.1'
def junitVersion = '5.5.0'
def restVersion = '4.1.2'
def apacheDrillVersion = '1.17.0'



repositories {
    jcenter()
    mavenCentral()
}


dependencies {
    compile group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: '1.9.13'
    compile group: 'com.opencsv', name: 'opencsv', version: '4.0'

    compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.12'

    compile group: 'org.apache.drill.tools', name: 'tools-parent', version: "${apacheDrillVersion}", ext: 'pom'

    compile group: 'de.monochromata.cucumber', name: 'reporting-plugin', version: '3.0.9'
    
    testCompile group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '6.1.0.jre8'

    compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.7.0'

    compile 'org.modelmapper:modelmapper:2.3.3'

    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('org.springframework.boot:spring-boot-starter-data-jpa')
    
    testImplementation "io.cucumber:cucumber-java:${cucumberVersion}"
    testImplementation "io.cucumber:cucumber-junit:${cucumberVersion}"
    testImplementation "io.rest-assured:rest-assured:${restVersion}"
    testImplementation "io.rest-assured:json-path:${restVersion}"
    testImplementation "io.rest-assured:xml-path:${restVersion}"
    testImplementation "io.rest-assured:json-schema-validator:${restVersion}"

    testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
    testRuntimeOnly "org.junit.vintage:junit-vintage-engine:${junitVersion}"
    implementation 'junit:junit:4.12'

    compileOnly 'org.projectlombok:lombok:1.18.12'
    annotationProcessor 'org.projectlombok:lombok:1.18.12'

    testCompileOnly 'org.projectlombok:lombok:1.18.12'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'

    implementation 'org.mapstruct:mapstruct:1.4.1.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.1.Final'

    compile group: 'org.hibernate', name: 'hibernate-gradle-plugin', version: '5.4.22.Final'
}


configurations {
    cucumberRuntime {
        extendsFrom testImplementation
    }
}

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath =  configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin',
                    'pretty',
                    '--glue',
                    'gradle.cucumber',
                    'src/test/resources/features',

            ]
        }
    }
}


test {
    //useJUnitPlatform()
    systemProperty "cucumber.options", System.properties.getProperty("cucumber.options")
}

RESTAssured 依赖声明(续)

testImplementation "io.rest-assured:rest-assured:${restVersion}"
testImplementation "io.rest-assured:json-path:${restVersion}"
testImplementation "io.rest-assured:xml-path:${restVersion}"
testImplementation "io.rest-assured:json-schema-validator:${restVersion}"

其背后的原因可以在link中找到:java.lang.NoClassDefFoundError: io/restassured/mapper/factory/GsonObjectMapperFactory

懒得上link的朋友,这里解释一下:

The root cause of this is rest-assured *ObjectMapperFactory package names changing, for example between versions 3.x and 4.x.

For anyone coming across this after the release of rest-assured 4.0.0, this problem can appear in Spring Boot projects - caused by a version mismatch between rest-assured and its transitive dependencies on json-path and xml-path in the spring-boot-dependencies bom.

If you specify a dependency io.rest-assured:rest-assured:4.0.0, you also need to explicitly include io.rest-assured:json-path:4.0.0 and io.rest-assured:xml-path:4.0.0, otherwise spring-boot-dependencies will pull in version 3.1.1 with the old *ObjectMapperFactory package names.