2

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.

Dan
  • 3,647
  • 5
  • 20
  • 26
quiram
  • 746
  • 1
  • 7
  • 18

3 Answers3

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.

quiram
  • 746
  • 1
  • 7
  • 18
1

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>
David J.
  • 11
  • 2
  • 1
    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](/help/how-to-answer). – Community Jul 08 '22 at 14:31
  • 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. – quiram Jul 20 '22 at 17:21
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

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32753726) – Kumar Saurabh Sep 23 '22 at 13:20
  • 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](/help/how-to-answer). – Community Sep 23 '22 at 18:10