marcin_koss
marcin_koss

Reputation: 5882

Issue with using mockito with mock-maker-inline in Spring boot application

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

Upvotes: 6

Views: 15881

Answers (4)

Kasi dronadula
Kasi dronadula

Reputation: 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

Upvotes: 0

Ashish Lahoti
Ashish Lahoti

Reputation: 1018

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>

Upvotes: 5

developer_hatch
developer_hatch

Reputation: 16224

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

Upvotes: 1

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44032

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?

Upvotes: 5

Related Questions