Search code examples
javagradleintellij-idearest-assuredrest-assured-jsonpath

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


I'm trying to run a Cucumber test-case scenario and received an error after compilation in IntelliJ-IDEA. I have posted the build.gradle file here. I'm not sure why I'm getting this error as soon as I run the program. Can somebody enlighten me on how to fix this issue? I've been getting this error for more than a day and still no solution.

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

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")
}

Note: I have removed redundant parts of the error.


Solution

  • I've managed to solve the java.lang.NoClassDefFoundError: io/restassured/path/xml/mapper/factory/JAXBObjectMapperFactory error by explicitly declaring the json-path and xml-pathin the build.gradle file.

    Here's how I've declared it.

    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")
    }
    

    Declaration of RESTAssured dependencies(cont'd)

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

    The reason behind it can be found in this link: java.lang.NoClassDefFoundError: io/restassured/mapper/factory/GsonObjectMapperFactory

    For those who are lazy to visit the link, here's the explanation:

    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.