繁体   English   中英

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文件。 我不确定为什么我一运行程序就会收到这个错误。 有人可以启发我如何解决这个问题吗? 我已经收到这个错误超过一天了,但仍然没有解决方案。

错误堆栈跟踪

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$1.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$1.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$1.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$1.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

构建.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错误。

这是我如何声明的。

构建.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}"

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

对于那些懒得访问链接的人,这里是解释:

其根本原因是放心的 *ObjectMapperFactory 包名称更改,例如在版本 3.x 和 4.x 之间。

对于在发布 rest-assured 4.0.0 之后遇到此问题的任何人,此问题可能会出现在 Spring Boot 项目中 - 由 rest-assured 及其对 json-path 和 xml-path 中的传递依赖项之间的版本不匹配引起 -引导依赖项 bom。

如果指定依赖项 io.rest-assured:rest-assured:4.0.0,则还需要明确包含 io.rest-assured:json-path:4.0.0 和 io.rest-assured:xml-path:4.0 .0,否则 spring-boot-dependencies 将使用旧的 *ObjectMapperFactory 包名称引入 3.1.1 版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM