Search code examples
kotlingradleintellij-ideaquarkusmockk

Why am I getting "java.lang.NoClassDefFoundError: Could not initialize class io.mockk.impl.JvmMockKGateway" when using quarkusDev task in IntelliJ?


I am using Gradle 7.5, Quarkus 2.12.3 and mockk 1.13.3. When I try to run quarkusDev task from command line and then start continuous testing (by pressing r), then all tests pass OK.

However, when I do the same as from IntelliJ (as gradle run configuration), all tests fail with error:

java.lang.NoClassDefFoundError: Could not initialize class io.mockk.impl.JvmMockKGateway

How can I fix that?


Solution

  • Masked thrown exception

    After much debugging I found the problem. The thrown exception actually originates in HotSpotVirtualMachine.java and is thrown during attachment of ByteBuddy as a java agent. Here is the relevant code;

    // The tool should be a different VM to the target. This check will
    // eventually be enforced by the target VM.
    if (!ALLOW_ATTACH_SELF && (pid == 0 || pid == CURRENT_PID)) {
         throw new IOException("Can not attach to current VM");
    }
    

    Turning check off

    So the check can be turned off by setting ALLOW_ATTACH_SELF constant to true. The constant is set from a system property named jdk.attach.allowAttachSelf:

    String s = VM.getSavedProperty("jdk.attach.allowAttachSelf");
    ALLOW_ATTACH_SELF = "".equals(s) || Boolean.parseBoolean(s);
    

    So, in my case, I simply added the following JVM argument to my gradle file and tests started to pass:

    tasks.quarkusDev {
        jvmArgs += "-Djdk.attach.allowAttachSelf"
    }