mockito icon indicating copy to clipboard operation
mockito copied to clipboard

JDK does not supply a working agent attachment mechanism [eclipse-temurin-23] [Docker]

Open eroval opened this issue 1 year ago • 3 comments

  • [x] The mockito message in the stacktrace have useful information, but it didn't help
  • [x] The problematic code (if that's possible) is copied here; Note that some configuration are impossible to mock via Mockito
  • [x] Provide versions (mockito / jdk / os / any other relevant information)
  • [ ] Provide a Short, Self Contained, Correct (Compilable), Example of the issue (same as any question on stackoverflow.com)
  • [ ] Read the contributing guide

Hello everyone,

I have been having trouble working with Mockito inside Docker. I am using:

  • image
    • maven:3-eclipse-temurin-23.
  • env variables
    • JDK_JAVA_OPTIONS: -Djdk.attach.allowAttachSelf=true -Djava.io.tmpdir=/tmp -Djdk.lang.Process.launchMechanism=vfork
    • JAVA_TOOL_OPTIONS: -Djdk.attach.allowAttachSelf=true -Djava.io.tmpdir=/tmp -Djdk.lang.Process.launchMechanism=vfork

Testing:

I remotely attach to the container through vs code (in order to have full capability) and run the tests like so: image

Log:

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null) at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:85) at jdk.proxy2/jdk.proxy2.$Proxy130.getHandler(Unknown Source) at org.mockito.internal.util.MockUtil.getMockHandlerOrNull(MockUtil.java:158) at org.mockito.internal.util.MockUtil.isMock(MockUtil.java:147) at org.mockito.internal.util.DefaultMockingDetails.isMock(DefaultMockingDetails.java:32) at org.springframework.boot.test.mock.mockito.MockReset.get(MockReset.java:106) at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:85) at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:73) at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.beforeTestMethod(ResetMocksTestExecutionListener.java:60) at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:320) at org.springframework.test.context.junit.jupiter.SpringExtension.beforeEach(SpringExtension.java:240) at java.base/java.util.ArrayList.forEach(ArrayList.java:1597) at java.base/java.util.ArrayList.forEach(ArrayList.java:1597) Suppressed: java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null) at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:85) at jdk.proxy2/jdk.proxy2.$Proxy130.getHandler(Unknown Source) at org.mockito.internal.util.MockUtil.getMockHandlerOrNull(MockUtil.java:158) at org.mockito.internal.util.MockUtil.isMock(MockUtil.java:147) at org.mockito.internal.util.DefaultMockingDetails.isMock(DefaultMockingDetails.java:32) at org.springframework.boot.test.mock.mockito.MockReset.get(MockReset.java:106) at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:85) at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:73) at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.afterTestMethod(ResetMocksTestExecutionListener.java:67) at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:487) at org.springframework.test.context.junit.jupiter.SpringExtension.afterEach(SpringExtension.java:278) ... 2 more Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in java.lang.CompoundEnumeration@72c108ac at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:56) at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:65) at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:49) at org.mockito.internal.configuration.plugins.PluginRegistry.(PluginRegistry.java:29) at org.mockito.internal.configuration.plugins.Plugins.(Plugins.java:26) at org.mockito.internal.MockitoCore.(MockitoCore.java:71) at org.mockito.Mockito.(Mockito.java:1683) at org.springframework.boot.test.mock.mockito.MockReset.get(MockReset.java:105) at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:85) at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:73) at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.beforeTestMethod(ResetMocksTestExecutionListener.java:60) at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:320) at org.springframework.test.context.junit.jupiter.SpringExtension.beforeEach(SpringExtension.java:240) ... 2 more Caused by: java.lang.reflect.InvocationTargetException at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:501) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:485) at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:51) ... 14 more Caused by: org.mockito.exceptions.base.MockitoInitializationException: Could not initialize inline Byte Buddy mock maker. It appears as if your JDK does not supply a working agent attachment mechanism. Java : 23 JVM vendor name : Eclipse Adoptium JVM vendor version : 23.0.1+11 JVM name : OpenJDK 64-Bit Server VM JVM version : 23.0.1+11 JVM info : mixed mode, sharing OS name : Linux OS version : 5.15.153.1-microsoft-standard-WSL2 at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.(InlineDelegateByteBuddyMockMaker.java:260) at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.(InlineByteBuddyMockMaker.java:23) ... 17 more Caused by: java.lang.IllegalStateException: Error during attachment using: net.bytebuddy.agent.ByteBuddyAgent$AttachmentProvider$Compound@3ce42613 at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:611) at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:584) at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:536) at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:513) at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.(InlineDelegateByteBuddyMockMaker.java:133) ... 18 more Caused by: java.lang.reflect.InvocationTargetException at net.bytebuddy.agent.Attacher.install(Attacher.java:102) at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:606) ... 22 more Caused by: com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file /tmp/.java_pid10424: target process 10424 doesn't respond within 10500ms or HotSpot VM not loaded at jdk.attach/sun.tools.attach.VirtualMachineImpl.(VirtualMachineImpl.java:99) at jdk.attach/sun.tools.attach.AttachProviderImpl.attachVirtualMachine(AttachProviderImpl.java:58) at jdk.attach/com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:207) ... 25 more Caused by: [CIRCULAR REFERENCE: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in java.lang.CompoundEnumeration@72c108ac]

Dependency Tree:

[INFO] com.deniszangarov:ethapi:jar:0.0.1-SNAPSHOT [INFO] +- org.modelmapper:modelmapper:jar:3.2.1:compile [INFO] +- org.web3j:core:jar:4.9.7:compile [INFO] | +- org.web3j:abi:jar:4.9.7:compile [INFO] | | - org.web3j:utils:jar:4.9.7:compile [INFO] | | - org.bouncycastle:bcprov-jdk15on:jar:1.70:compile [INFO] | +- org.web3j:crypto:jar:4.9.7:compile [INFO] | | - org.web3j:rlp:jar:4.9.7:compile [INFO] | +- org.web3j:tuples:jar:4.9.7:compile [INFO] | +- com.github.jnr:jnr-unixsocket:jar:0.38.17:compile [INFO] | | +- com.github.jnr:jnr-ffi:jar:2.2.11:compile [INFO] | | | +- com.github.jnr:jffi:jar:1.3.9:compile [INFO] | | | +- com.github.jnr:jffi:jar:native:1.3.9:runtime [INFO] | | | +- org.ow2.asm:asm:jar:9.2:compile [INFO] | | | +- org.ow2.asm:asm-commons:jar:9.2:compile [INFO] | | | +- org.ow2.asm:asm-analysis:jar:9.2:compile [INFO] | | | +- org.ow2.asm:asm-tree:jar:9.2:compile [INFO] | | | +- org.ow2.asm:asm-util:jar:9.2:compile [INFO] | | | +- com.github.jnr:jnr-a64asm:jar:1.0.0:compile [INFO] | | | - com.github.jnr:jnr-x86asm:jar:1.0.2:compile [INFO] | | +- com.github.jnr:jnr-constants:jar:0.10.3:compile [INFO] | | +- com.github.jnr:jnr-enxio:jar:0.32.13:compile [INFO] | | - com.github.jnr:jnr-posix:jar:3.1.15:compile [INFO] | +- com.squareup.okhttp3:okhttp:jar:4.12.0:compile [INFO] | | +- com.squareup.okio:okio:jar:3.6.0:compile [INFO] | | | - com.squareup.okio:okio-jvm:jar:3.6.0:compile [INFO] | | | - org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.9.25:compile [INFO] | | - org.jetbrains.kotlin:kotlin-stdlib-jdk8:jar:1.9.25:compile [INFO] | | +- org.jetbrains.kotlin:kotlin-stdlib:jar:1.9.25:compile [INFO] | | | - org.jetbrains:annotations:jar:13.0:compile [INFO] | | - org.jetbrains.kotlin:kotlin-stdlib-jdk7:jar:1.9.25:compile [INFO] | +- com.squareup.okhttp3:logging-interceptor:jar:4.12.0:compile [INFO] | +- io.reactivex.rxjava2:rxjava:jar:2.2.2:compile [INFO] | | - org.reactivestreams:reactive-streams:jar:1.0.4:compile [INFO] | +- org.java-websocket:Java-WebSocket:jar:1.5.3:compile [INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.17.2:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.17.2:compile [INFO] | | - com.fasterxml.jackson.core:jackson-core:jar:2.17.2:compile [INFO] | - org.slf4j:slf4j-api:jar:2.0.16:compile [INFO] +- org.projectlombok:lombok:jar:1.18.34:provided [INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:3.3.5:compile [INFO] | +- org.springframework.boot:spring-boot-starter-aop:jar:3.3.5:compile [INFO] | | +- org.springframework:spring-aop:jar:6.1.14:compile [INFO] | | - org.aspectj:aspectjweaver:jar:1.9.22.1:compile [INFO] | +- org.springframework.boot:spring-boot-starter-jdbc:jar:3.3.5:compile [INFO] | | +- com.zaxxer:HikariCP:jar:5.1.0:compile [INFO] | | - org.springframework:spring-jdbc:jar:6.1.14:compile [INFO] | +- org.hibernate.orm:hibernate-core:jar:6.5.3.Final:compile [INFO] | | +- jakarta.persistence:jakarta.persistence-api:jar:3.1.0:compile [INFO] | | +- jakarta.transaction:jakarta.transaction-api:jar:2.0.1:compile [INFO] | | +- org.jboss.logging:jboss-logging:jar:3.5.3.Final:runtime [INFO] | | +- org.hibernate.common:hibernate-commons-annotations:jar:6.0.6.Final:runtime [INFO] | | +- io.smallrye:jandex:jar:3.1.2:runtime [INFO] | | +- com.fasterxml:classmate:jar:1.7.0:runtime [INFO] | | +- org.glassfish.jaxb:jaxb-runtime:jar:4.0.5:runtime [INFO] | | | - org.glassfish.jaxb:jaxb-core:jar:4.0.5:runtime [INFO] | | | +- org.eclipse.angus:angus-activation:jar:2.0.2:runtime [INFO] | | | +- org.glassfish.jaxb:txw2:jar:4.0.5:runtime [INFO] | | | - com.sun.istack:istack-commons-runtime:jar:4.1.2:runtime [INFO] | | +- jakarta.inject:jakarta.inject-api:jar:2.0.1:runtime [INFO] | | - org.antlr:antlr4-runtime:jar:4.13.0:compile [INFO] | +- org.springframework.data:spring-data-jpa:jar:3.3.5:compile [INFO] | | +- org.springframework.data:spring-data-commons:jar:3.3.5:compile [INFO] | | +- org.springframework:spring-orm:jar:6.1.14:compile [INFO] | | +- org.springframework:spring-context:jar:6.1.14:compile [INFO] | | +- org.springframework:spring-tx:jar:6.1.14:compile [INFO] | | +- org.springframework:spring-beans:jar:6.1.14:compile [INFO] | | - jakarta.annotation:jakarta.annotation-api:jar:2.1.1:compile [INFO] | - org.springframework:spring-aspects:jar:6.1.14:compile [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:3.3.5:compile [INFO] | +- org.springframework.boot:spring-boot-starter:jar:3.3.5:compile [INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:3.3.5:compile [INFO] | | | +- ch.qos.logback:logback-classic:jar:1.5.11:compile [INFO] | | | | - ch.qos.logback:logback-core:jar:1.5.11:compile [INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.23.1:compile [INFO] | | | | - org.apache.logging.log4j:log4j-api:jar:2.23.1:compile [INFO] | | | - org.slf4j:jul-to-slf4j:jar:2.0.16:compile [INFO] | | - org.yaml:snakeyaml:jar:2.2:compile [INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:3.3.5:compile [INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.17.2:compile [INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.17.2:compile [INFO] | | - com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.17.2:compile [INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:3.3.5:compile [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:10.1.31:compile [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:10.1.31:compile [INFO] | | - org.apache.tomcat.embed:tomcat-embed-websocket:jar:10.1.31:compile [INFO] | +- org.springframework:spring-web:jar:6.1.14:compile [INFO] | | - io.micrometer:micrometer-observation:jar:1.13.6:compile [INFO] | | - io.micrometer:micrometer-commons:jar:1.13.6:compile [INFO] | - org.springframework:spring-webmvc:jar:6.1.14:compile [INFO] | - org.springframework:spring-expression:jar:6.1.14:compile [INFO] +- com.mysql:mysql-connector-j:jar:8.3.0:compile [INFO] +- org.springframework.boot:spring-boot-devtools:jar:3.3.5:runtime [INFO] | +- org.springframework.boot:spring-boot:jar:3.3.5:compile [INFO] | - org.springframework.boot:spring-boot-autoconfigure:jar:3.3.5:compile [INFO] +- org.springframework.boot:spring-boot-starter-test:jar:3.3.5:test [INFO] | +- org.springframework.boot:spring-boot-test:jar:3.3.5:test [INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:3.3.5:test [INFO] | +- com.jayway.jsonpath:json-path:jar:2.9.0:test [INFO] | +- jakarta.xml.bind:jakarta.xml.bind-api:jar:4.0.2:runtime [INFO] | | - jakarta.activation:jakarta.activation-api:jar:2.1.3:runtime [INFO] | +- net.minidev:json-smart:jar:2.5.1:test [INFO] | | - net.minidev:accessors-smart:jar:2.5.1:test [INFO] | +- org.assertj:assertj-core:jar:3.25.3:test [INFO] | +- org.awaitility:awaitility:jar:4.2.2:test [INFO] | +- org.hamcrest:hamcrest:jar:2.2:test [INFO] | +- org.junit.jupiter:junit-jupiter:jar:5.10.5:test [INFO] | | +- org.junit.jupiter:junit-jupiter-api:jar:5.10.5:test [INFO] | | | +- org.opentest4j:opentest4j:jar:1.3.0:test [INFO] | | | +- org.junit.platform:junit-platform-commons:jar:1.10.5:test [INFO] | | | - org.apiguardian:apiguardian-api:jar:1.1.2:test [INFO] | | +- org.junit.jupiter:junit-jupiter-params:jar:5.10.5:test [INFO] | | - org.junit.jupiter:junit-jupiter-engine:jar:5.10.5:test [INFO] | | - org.junit.platform:junit-platform-engine:jar:1.10.5:test [INFO] | +- org.mockito:mockito-junit-jupiter:jar:5.11.0:test [INFO] | +- org.skyscreamer:jsonassert:jar:1.5.3:test [INFO] | | - com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test [INFO] | +- org.springframework:spring-core:jar:6.1.14:compile [INFO] | | - org.springframework:spring-jcl:jar:6.1.14:compile [INFO] | +- org.springframework:spring-test:jar:6.1.14:test [INFO] | - org.xmlunit:xmlunit-core:jar:2.9.1:test [INFO] +- net.java.dev.jna:jna:jar:5.14.0:test [INFO] +- org.objenesis:objenesis:jar:3.3:compile [INFO] - org.mockito:mockito-core:jar:5.11.0:test [INFO] +- net.bytebuddy:byte-buddy:jar:1.14.19:runtime [INFO] - net.bytebuddy:byte-buddy-agent:jar:1.14.19:test

I cannot find what the issue is. I've tried debugging but it seems that the agent simply cannot attach. Step by step debugging I located that there are byte-buddy-agent-1.14.19.jars located here and there is an agent:

image

eroval avatar Nov 05 '24 13:11 eroval

Okay - weirdly, I solved it partially. However I feel like I'm over-engineering this and it shouldn't be so hard to set up. I updated to the newest bytebuddy & mockito but that was NOT enough. For some reason the thing wasn't picking up the agent correctly even after specifying arglines, options & etc. So what I did was:

  • created file test/resources/mockito-extensions/org.mockito.plugins.MockMaker with the following contents:
mock-maker-inline

Manually I successfully ran the tests:

  • docker exec -it <container_id> /bin/bash to log to the container interactively
  • mvn test -DargLine="-javaagent:/root/.m2/repository/net/bytebuddy/byte-buddy-agent/1.15.4/byte-buddy-agent-1.15.4.jar" to specify the byte buddy agent explicitly.

To automate the above process to work with just docker exec <container> mvn test:

  • Inside pom.xml, I overwrote the properties tab to specify bytebuddy version, added the maven surefire plugin with the location of bytebuddy as argline:
...
<properties>
   <byte-buddy.version>1.15.4</byte-buddy.version>
</properties>

...

<build>
   <plugins>  
   ...
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <version>3.5.2</version> 
         <configuration>
            <argLine>-javaagent:/root/.m2/repository/net/bytebuddy/byte-buddy-agent/${byte-buddy.version}/byte-buddy-agent-${byte-buddy.version}.jar</argLine>
         </configuration>
      </plugin>
   ...
   </plugins>  
</build>

Now the tests run successfully, however, I still CANNOT run these through VS Code Remote Container and get the same error which is a bummer since I want to use the advanced debugging functions.

eroval avatar Nov 05 '24 16:11 eroval

I am facing this exact same issue, and your solution of pointing the mvn test command at the byte-buddy jar file also allows my tests to run!

In my case:

  1. I am on an Apple Silicon M2 MacBook Pro.
  2. I am running Docker Desktop 4.35.1 (173168).
  3. I use an X86 Linux container to perform my build (therefore, it's emulated).
  4. I have mock-maker-inline enabled.

Now, within the same Docker container running on X86 HW running Linux - the problem doesn't occur!

Ryan0751 avatar Nov 14 '24 13:11 Ryan0751

Versions of Java after Java 21 don't support dynamic agent attaching. The -javaagent is required.

jimshowalter avatar Mar 20 '25 18:03 jimshowalter