7

I have a Spring web application that can operate in “special” mode where mockito is used to spy on certain objects. Some of those objects are final (protobuf messages). I know, this may smell like a bad idea but lets say it’s an experiment. When enabling mock-maker-inline extension so final objects can be spied on I’m running into issues that seem to be related to byte buddy and loading some native libraries. When not using extension (and not spying on final classes) everything works as expected. Below is truncated stack trace.

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null) … Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@60cc2b75 … 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 : 1.8 JVM vendor name : AdoptOpenJDK JVM vendor version : 25.265-b01 JVM name : OpenJDK 64-Bit Server VM JVM version : 1.8.0_265-b01 JVM info : mixed mode OS name : Mac OS X OS version : 10.15.7 … Caused by: java.lang.IllegalStateException: Error during attachment using: net.bytebuddy.agent.ByteBuddyAgent$AttachmentProvider$Compound@633c165e … Caused by: java.lang.reflect.InvocationTargetException … Caused by: java.lang.UnsatisfiedLinkError: Native Library /Users/…/.sdkman/candidates/java/8.0.265.hs-adpt/jre/lib/libattach.dylib already loaded in another classloader 

4 Answers 4

9

I started facing this issue after upgrading to Java-21 and Spring-Boot-3.3.1.

Spring-Boot-3.3.1 use Mockito 5, which is having known issue with Java-17 onwards (https://github.com/mockito/mockito/releases/tag/v5.0.0)

The issue resolved after adding mockito-subclass dependency as mentioned in the link.

<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-subclass</artifactId> <version>5.11.0</version> <scope>test</scope> </dependency> 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I tried every solution on the web and no one worked for me until I find yours !
Thank you very much. It solved the issue.
5

This is an unfortunate limitation of JVMs that is solved in Java 9+. In Java 8-, the attachment library can only be bound by a single class loader. It seems as if Byte Buddy and another library attemts a self-attach (maybe EhCache) and bind the virtual machine API.

Can you identify what other library is self-attaching and possibly prevent this?

5 Comments

Thanks Rafael! actually net.sf.ehcache is used as a transitive dependency. What can I do to prevent self-attaching? Also, any guidance how to identify some other dependencies that may be self-attaching?
When trying with Java 11, I'm getting missing tools.jar. Is it just a matter of adding it to class path?
Not getting those errors anymore on Java 11 but looks like mock-maker-inline doesn't work now - Mockito cannot mock/spy because : final.... Using mockito-core:3.11.2 that uses byte-buddy:1.10.22.
That should not be the case. Have you tried the latest Mockito release?
Hi @RafaelWinterhalter, I am facing the exact same error as marcin_koss , after adding a couple of my company's internal dependencies..Can you please tell me how to identify some other dependencies that may be self-attaching. My project uses mockito-junit-jupiter and mockito-inline dependencies from org.mockito
1

In my case the project contained an older version of JMockit 'org.jmockit:jmockit:1.24' which was clashing with mockit-inline. Removing JMockit entirely or upgrading it to 1.49 resolves the problem

https://github.com/mockito/mockito/issues/1879#issuecomment-783326979

Comments

-1

Mockito is currently self-attaching to enable the inline-mock-maker. This will no longer work in future releases of the JDK. Please add Mockito as an agent to your build what is described in Mockito's documentation: https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#0.3 WARNING: A Java agent has been loaded dynamically (C:\Users\kasid.m2\repository\net\bytebuddy\byte-buddy-agent\1.15.11\byte-buddy-agent-1.15.11.jar) WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information WARNING: Dynamic loading of agents will be disallowed by default in a future release Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended 2

1 Comment

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.