java.lang.NoClassDefFoundError: jakarta/servlet/ServletConnection while wrinting junit

86 views Asked by At

java.lang.NoClassDefFoundError: jakarta/servlet/ServletConnection while wrinting junit

I am writing Junit for my Java projects. I am have an issue like

when I am removing This I am able to run test cases but not my application. and I am adding it I am able to run application not test cases. My few of the test cases are getting failed.

I am using Spring 3.14 with Java 17.

resolutionStrategy.eachDependency { details ->
    if (details.requested.group == 'jakarta.servlet') {
    details.useVersion '5.0.0'
    }
}

This is build.gradle file.

plugins {
    id 'org.springframework.boot' version '3.1.4'
    id 'io.spring.dependency-management' version '1.0.12.RELEASE'
    id 'java'
    id 'eclipse'
    id "com.diffplug.eclipse.apt" version "3.38.0"
    id 'jacoco'
    id 'org.sonarqube' version '3.3'
}
group = 'com.company.app'
version = '1.1.0'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-websocket'
        testImplementation.exclude group: 'org.projectlombok', module: 'lombok'

        if (project.hasProperty('runTests')) {
            testImplementation.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
        }
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'jakarta.servlet') {
            details.useVersion '5.0.0'
            }
        }
    }
    
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'
    implementation 'org.springframework.boot:spring-boot-starter-jetty'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    // Some other dependency 
    testImplementation 'org.springframework.security:spring-security-test'
    testImplementation 'org.eclipse.jetty:jetty-server:12.0.0'
    testImplementation 'jakarta.servlet:jakarta.servlet-api:6.0.0'
    testImplementation 'com.h2database:h2:1.4.200'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.mockito:mockito-core:3.12.4'
    testImplementation 'org.projectlombok:lombok:1.18.28'
    testImplementation 'org.projectlombok:lombok:1.18.28'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}
    


test {
    useJUnitPlatform()
}

tasks.withType(JavaCompile) {
    configure(options) {
        options.compilerArgs << '-Xlint:deprecation' << '-Xlint:unchecked'
    }
}

jacoco {
    toolVersion = "0.8.10"
}

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
    jacoco.excludes = ['jdk.internal.*']
}

test {
    useJUnitPlatform()
    finalizedBy jacocoTestReport
}



// some other configurations

Here is What I am getting in error :

java.lang.NoClassDefFoundError: jakarta/servlet/ServletConnection
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:210)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:130)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:241)
    at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:138)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$8(ClassBasedTestDescriptor.java:363)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:368)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$9(ClassBasedTestDescriptor.java:363)


Caused by: java.lang.ClassNotFoundException: jakarta.servlet.ServletConnection
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    ... 85 more

Note : I can't update to servlet 6 with hetty server api as many of my configuration are in use already.

0

There are 0 answers