java.lang.NoClassDefFoundError: io/mockk/proxy/jvm/advice/jvm/JvmMockKProxyInterceptor ERROR while mocking org.w3c.dom.Node using mockk

297 views Asked by At

I have a gradle project in kotlin using xml(javax.xml.bind:jaxb-api:2.3.1). I want to test my parsing logic , hence I need to mock my Node(org.w3c.dom.Node). How ever while mocking I am facing this error.

io/mockk/proxy/jvm/advice/jvm/JvmMockKProxyInterceptor java.lang.NoClassDefFoundError: io/mockk/proxy/jvm/advice/jvm/JvmMockKProxyInterceptor
at java.xml/org.w3c.dom.Node$Subclass0.getNodeName(Unknown Source)

My Code

build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.8.0"
    application
}

group = "me.shantiswaruptunga"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1")
    implementation("javax.xml.bind:jaxb-api:2.3.1")
    testImplementation(kotlin("test"))
    testImplementation("io.mockk:mockk:1.12.4")
}

tasks.test {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "1.8"
}

application {
    mainClass.set("MainKt")
}

XMLTest.kt

import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Test
import org.w3c.dom.Node

class XmlTest {
    @Test
    fun test2() {
        val node1 = mockk<Node>()
        every { node1.nodeName } returns "x"
    }
}

The same code was working with jdk 8 kotlin 1.4.31 I referred this mockk github issue but couldn't figure out the problem

2

There are 2 answers

0
Oscar On

Which version of gradle did you use? it might be important to check compatibility https://docs.gradle.org/current/userguide/compatibility.html.

Otherwise I used your example in a blank project creating with intellij and curiously I don't have this problem. This test case worked fine. code example

Two scenarios:

  • Problem has been resolved by a recent update of io.mockk:mockk library(or update on its library dependencies)

  • Problem is not identifiable with code example you provided

0
user1770426 On

It looks like this issue is not related with code or dependencies, but rather with toolchain.

I have faced exactly same issue, mocking the SSLServerSocket with Mockk.

The solution, one which has helped me, was to change jdk to some newer version (I was using Android Studio and has changed it into: Build Tools->Gradle->{project}->Gradle JDK).

After applying newer jdk, I was able to run my tests with mocks.