最近在跟着视频学习springboot搭建,使用 mvn clean install进行打包时出现了问题。
- 主要问题:最顶层的
IllegalStateException
是由于插件初始化失败引起的。 - 次要问题:这是因为
MockitoInitializationException
,即 Byte Buddy mock maker 无法初始化。 - 根本原因:这由一个
IllegalArgumentException
引起,提示 Java Instrumentation API 的问题。 - 循环引用:提供了更多关于异常循环的信息,但不是根本原因。
问题的根本原因
- JDK 版本:Java 22 版本可能不完全支持 Mockito 所需的代理附件机制。这可能是因为 Java 22 是相对较新的版本,而某些库(如 Mockito 和 Byte Buddy)可能还未完全适配。
解决办法
Mockito 5 的开发文档中提到 由于我们使用了 JVM 内部 API,我们发现 JDK 最新版本的问题/不兼容性有所增加。最值得注意的是,JDK 17 进行了一些与当前子类 mockmaker 不兼容的更改。git文档
对于Mockito 5+ 和 jdk 17+ 在内联模拟生成器将不起作用,可以使用子类模拟生成器。
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-subclass</artifactId>
<version>5.12.0</version>
</dependency>
将这部分代码添加到依赖中,重新运行,问题得到解决。
错误堆栈:
-------------------------------------------------------------------------------
Test set: com.example.demo3.Demo3ApplicationTests
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 7.256 s <<< FAILURE! -- in com.example.demo3.Demo3ApplicationTests
com.example.demo3.Demo3ApplicationTests.contextLoads -- Time elapsed: 0.468 s <<< ERROR!
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:85)
at jdk.proxy2/jdk.proxy2.$Proxy129.getHandler(Unknown Source)
at org.mockito.internal.util.MockUtil.getMockHandlerOrNull(MockUtil.java:158)
at org.mockito.internal.util.MockUtil.isMock(MockUtil.java:147)
at org.mockito.internal.util.DefaultMockingDetails.isMock(DefaultMockingDetails.java:32)
at org.springframework.boot.test.mock.mockito.MockReset.get(MockReset.java:106)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:85)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:73)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.beforeTestMethod(ResetMocksTestExecutionListener.java:60)
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:320)
at org.springframework.test.context.junit.jupiter.SpringExtension.beforeEach(SpringExtension.java:240)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1597)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1597)
Suppressed: java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:85)
at jdk.proxy2/jdk.proxy2.$Proxy129.getHandler(Unknown Source)
at org.mockito.internal.util.MockUtil.getMockHandlerOrNull(MockUtil.java:158)
at org.mockito.internal.util.MockUtil.isMock(MockUtil.java:147)
at org.mockito.internal.util.DefaultMockingDetails.isMock(DefaultMockingDetails.java:32)
at org.springframework.boot.test.mock.mockito.MockReset.get(MockReset.java:106)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:85)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:73)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.afterTestMethod(ResetMocksTestExecutionListener.java:67)
at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:487)
at org.springframework.test.context.junit.jupiter.SpringExtension.afterEach(SpringExtension.java:278)
... 2 more
Caused by: java.lang.IllegalStateException: Internal problem occurred, please report it. Mockito is unable to load the default implementation of class that is a part of Mockito distribution. Failed to load interface org.mockito.plugins.MockMaker
at org.mockito.internal.configuration.plugins.DefaultMockitoPlugins.create(DefaultMockitoPlugins.java:105)
at org.mockito.internal.configuration.plugins.DefaultMockitoPlugins.getDefaultPlugin(DefaultMockitoPlugins.java:79)
at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:75)
at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:49)
at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:29)
at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:26)
at org.mockito.internal.MockitoCore.<clinit>(MockitoCore.java:71)
at org.mockito.Mockito.<clinit>(Mockito.java:1683)
at org.springframework.boot.test.mock.mockito.MockReset.get(MockReset.java:105)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:85)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:73)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.beforeTestMethod(ResetMocksTestExecutionListener.java:60)
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:320)
at org.springframework.test.context.junit.jupiter.SpringExtension.beforeEach(SpringExtension.java:240)
... 2 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
at org.mockito.internal.configuration.plugins.DefaultMockitoPlugins.create(DefaultMockitoPlugins.java:103)
... 15 more
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 : 22
JVM vendor name : Oracle Corporation
JVM vendor version : 22.0.2+9-70
JVM name : OpenJDK 64-Bit Server VM
JVM version : 22.0.2+9-70
JVM info : mixed mode, sharing
OS name : Windows 10
OS version : 10.0
at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.<init>(InlineDelegateByteBuddyMockMaker.java:260)
at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<init>(InlineByteBuddyMockMaker.java:23)
... 18 more
Caused by: java.lang.IllegalArgumentException
at java.instrument/sun.instrument.InstrumentationImpl.appendToClassLoaderSearch0(Native Method)
at java.instrument/sun.instrument.InstrumentationImpl.appendToBootstrapClassLoaderSearch(InstrumentationImpl.java:280)
at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.<clinit>(InlineDelegateByteBuddyMockMaker.java:177)
... 19 more
Caused by: [CIRCULAR REFERENCE: java.lang.IllegalStateException: Internal problem occurred, please report it. Mockito is unable to load the default implementation of class that is a part of Mockito distribution. Failed to load interface org.mockito.plugins.MockMaker]