6

I have a bunch of Spring Boot 2.5.6 applications that use Mockito for unit testing. The version of Mockito that is used is the one that comes shipped within SB itself (3.9.0). The JDK is OpenJDK 11.0.12+0. Everything was working fine until yesterday, this morning I suddenly cannot run any Mockito-based tests, all of them fail with the following:

Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in java.lang.CompoundEnumeration@7c51f34b Caused by: java.lang.reflect.InvocationTargetException 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 : 11 JVM vendor name : Homebrew JVM vendor version : 11.0.12+0 JVM name : OpenJDK 64-Bit Server VM JVM version : 11.0.12+0 JVM info : mixed mode OS name : Mac OS X OS version : 12.0.1 Caused by: java.lang.IllegalStateException: Could not self-attach to current VM using external process 

I cannot identify anything that has changed between yesterday and today.

Things I have tried with no success:

  • Re-run with Java 17.
  • Re-install Java 11.
  • Install a JDK 11 from a different vendor (Microsoft OpenJDK)
  • Add -Djdk.attach.allowAttachSelf=true, both to command line and to <argLine> parameter within surefire configuration, as suggested here
  • Add -XX:+StartAttachListener as suggested here
  • Restart the computer
  • Ensure the firewall allows the java process to receive incoming connections as per here
  • (Temporarily) stop any security software that may prevent connections from happening
  • Review JAVA_HOME and the different Java installations on my machine in case the process was trying to attach to the wrong Java as per here

In case it matters, this is on an MBP M1. Other members of the team are able to build with no problem.

4 Answers 4

4

I've recently had the same issue with mockito-inline and JDK 11.0.11+9.

The solution was to explicitly attach the byte-buddy-agent to the JVM when running tests:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <argLine>-javaagent:${project.basedir}/target/byte-buddy-agent-${byte-buddy.version}.jar</argLine> </configuration> </plugin> 

To do this you may also need to copy the agent jar into /target:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>process-resources</phase> <goals> <goal>copy</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>net.bytebuddy</groupId> <artifactId>byte-buddy-agent</artifactId> <outputDirectory>${project.build.directory}</outputDirectory> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> 
Sign up to request clarification or add additional context in comments.

3 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
Thanks for the tip, however, I've seen those issues related to mockito-inline but I'm not using mockito-inline, so there should be no need to manually attach the agent. Also, no one else in the team is facing this issue, so I don't think that build configuration is the problem.
A similar, but nicer, solution (and other tips) can be found in this thread github.com/mockito/mockito/issues/3037#issuecomment-1930132240 that uses the maven-dependency-plugin properties goal.
1

As pointed out by @david-j, you can try to attach the byte-buddy-agent explicitly.

A slighter nicer why to do it (and other tips) is found in this thread https://github.com/mockito/mockito/issues/3037#issuecomment-1930132240 and it is worth mentioning. :)

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.6.1</version> <executions> <execution> <goals> <goal>properties</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.5</version> <configuration> <argLine>@{argLine} -javaagent:${net.bytebuddy:byte-buddy-agent:jar}</argLine> </configuration> </plugin> 

1 Comment

However in the provided PR they have updated the documentation and recommended a slightly different -javaagent:${org.mockito:mockito-core:jar}. See github.com/mockito/mockito/pull/3437/…
1

Not sure how much of an answer this will be, but the issue is now (seemingly) self-fixed... I'll describe what I've done and what happened in case it helps other users experiencing similar issues.

After unsuccessfully trying all the things described in the question, I had to restart the laptop (again) for an unrelated problem (Zoom wouldn't start up). Upon restart the laptop simply crashed and restarted again on its own. Then it did the same a second time. Upon the third automatic restart, I was no longer experiencing issues building my Java applications.

My guess is that the NVRAM got somehow corrupted and this was affecting the JVM's ability to accept incoming requests for attachment. On a MBP M1 one cannot reset the NVRAM as with Intel models (pressing Cmd + Opt + R + P), instead the NVRAM is supposed to self-heal when it detects something wrong. I suppose after crashing the MBP finally decided to repair the NVRAM and that's why the build starting working again.

UPDATE: I have now encountered this issue a few more times and every time the "solution" was to restart the laptop a few times until it crashed. After that the issue would disappear. This adds weight to the idea that there is some inherent instability with the combination of hardware and software specified in the question. I'll leave this as an "answered" question in case anybody else has the same issue.

UPDATE 2: This may be pure coincidence, but the issue seems to be exacerbated by the usage of an additional screen, particularly using an iPad via Sidecar.

UPDATE 3: I haven't experienced this issue for a long while now so I'm guessing the issue was caused by a set of underlying conditions that are now fixed...

1 Comment

My Intellij suddenly threw that error. I had JDK 11 and 17; I switched the SDK version from 11 to 17 for a project; and my PC was a bit low of free memory. I simply restarted my Intellij and the issue was gone :s
0

I got the same issue when I installed multiple java in my local system and didn't appropriately maintain, the solution worked for me: removed all installed java and reinstall it again

2 Comments

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.