4

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
marcin_koss
  • 5,763
  • 10
  • 46
  • 65

2 Answers2

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?

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • 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? – marcin_koss Jul 30 '21 at 21:06
  • When trying with Java 11, I'm getting missing tools.jar. Is it just a matter of adding it to class path? – marcin_koss Jul 30 '21 at 21:09
  • 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. – marcin_koss Jul 30 '21 at 22:27
  • That should not be the case. Have you tried the latest Mockito release? – Rafael Winterhalter Jul 30 '21 at 22:29
  • 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 – user3903418 Jul 20 '23 at 08:28
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

developer_hatch
  • 15,898
  • 3
  • 42
  • 75