添加公司内部依赖后出现Mockito inline Byte Buddy初始化异常,求解决
解决Mockito inline mock初始化失败(AttachNotSupportedException)问题
问题背景
项目使用以下Mockito依赖:
<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-inline</artifactId> <version>3.12.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <scope>test</scope> </dependency>
添加公司内部依赖后出现如下异常(已确认IntelliJ IDEA使用JDK而非JRE):
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 : Azul Systems, Inc. JVM vendor version : 25.232-b18 JVM name : OpenJDK 64-Bit Server VM JVM version : 1.8.0_232-b18 JVM info : mixed mode OS name : Mac OS X OS version : 10.16 at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.<init>(InlineDelegateByteBuddyMockMaker.java:246) at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<init>(InlineByteBuddyMockMaker.java:25) ... 73 more Caused by: java.lang.IllegalStateException: Error during attachment using: net.bytebuddy.agent.ByteBuddyAgent$AttachmentProvider$Compound@d70f722 at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:638) at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:611) at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:563) at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:540) at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.<clinit>(InlineDelegateByteBuddyMockMaker.java:117) ... 74 more Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at net.bytebuddy.agent.Attacher.install(Attacher.java:102) at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:633) ... 78 more Caused by: com.sun.tools.attach.AttachNotSupportedException: no providers installed at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:208) ... 84 more [ERROR] testGenerateConnectLiteUrlServerError Time elapsed: 0 s <<< ERROR! java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
解决方案
1. 排查ByteBuddy版本冲突
Mockito-inline依赖的ByteBuddy版本可能被内部依赖覆盖,导致版本不兼容。执行Maven命令查看依赖树:
mvn dependency:tree -Dincludes=net.bytebuddy:byte-buddy,net.bytebuddy:byte-buddy-agent
若发现内部依赖引入了不兼容版本,在项目dependencyManagement中强制指定兼容版本(Mockito 3.12.4对应ByteBuddy 1.10.19):
<dependencyManagement> <dependencies> <dependency> <groupId>net.bytebuddy</groupId> <artifactId>byte-buddy</artifactId> <version>1.10.19</version> <scope>test</scope> </dependency> <dependency> <groupId>net.bytebuddy</groupId> <artifactId>byte-buddy-agent</artifactId> <version>1.10.19</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement>
2. 补充JDK attach相关依赖
部分内部依赖可能打包时移除了com.sun.tools.attach包类,显式添加tools.jar依赖(仅测试范围):
<dependency> <groupId>com.sun</groupId> <artifactId>tools</artifactId> <version>1.8.0</version> <scope>system</scope> <systemPath>${java.home}/../lib/tools.jar</systemPath> </dependency>
注:Azul OpenJDK的tools.jar路径需根据实际安装目录调整。
3. 切换Mockito mock实现方式
绕过ByteBuddy的agent attach机制,改用子类继承模式创建mock:
在src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker文件中,将内容从mock-maker-inline修改为:
mock-maker-subclass
4. 检查JVM参数是否被篡改
内部依赖可能通过测试插件修改JVM参数,禁用了agent attach功能。检查测试运行参数,移除如下类似影响agent加载的配置:
-Xbootclasspath/a:
内容的提问来源于stack exchange,提问作者user3903418
相关产品推荐
相关产品推荐

