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

3

5

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
Lawrenson answered 29/7, 2021 at 18:2 Comment(0)
B
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?

Blackmarket answered 30/7, 2021 at 19:50 Comment(5)
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?Lawrenson
When trying with Java 11, I'm getting missing tools.jar. Is it just a matter of adding it to class path?Lawrenson
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.Lawrenson
That should not be the case. Have you tried the latest Mockito release?Blackmarket
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.mockitoToadeater
N
3

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>
</dependency>
Neotype answered 12/7 at 12:10 Comment(0)
R
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

Rheumatoid answered 26/1, 2022 at 22:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.