Mockito support JDK 21 - Byte Buddy

2,997 views
Skip to first unread message

Pablo

unread,
Jan 8, 2025, 8:04:13 AMJan 8
to mockito
Hi All I am migrating all my code to JDK 21. I have just run the tests and I have found this issue with Byte Buddy. Looks like as this version doesn't support JDK 21.

I am using Mockito-inline 5.2.0, Do you know if exist any mockito version that support JDK 21? or if there is any workaround?

Best Regards

This problem should never occur for javac-compiled classes. This problem has been observed for classes that are:
 - Compiled by older versions of scalac
 - Classes that are part of the Android distribution
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.triggerRetransformation(InlineBytecodeGenerator.java:285)
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.mockClassStatic(InlineBytecodeGenerator.java:226)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClassStatic(TypeCachingBytecodeGenerator.java:63)
at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.createStaticMock(InlineDelegateByteBuddyMockMaker.java:566)
at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.createStaticMock(InlineByteBuddyMockMaker.java:83)
at org.mockito.internal.util.MockUtil.createStaticMock(MockUtil.java:202)
at org.mockito.internal.MockitoCore.mockStatic(MockitoCore.java:134)
at org.mockito.Mockito.mockStatic(Mockito.java:2307)
at org.mockito.Mockito.mockStatic(Mockito.java:2244)
... 68 more
Caused by: java.lang.IllegalArgumentException: Java 21 (65) is not supported by the current version of Byte Buddy which officially supports Java 20 (64) - update Byte Buddy or set net.bytebuddy.experimental as a VM property
at net.bytebuddy.utility.OpenedClassReader.of(OpenedClassReader.java:96)
at net.bytebuddy.dynamic.scaffold.TypeWriter$Default$ForInlining.create(TypeWriter.java:4011)
at net.bytebuddy.dynamic.scaffold.TypeWriter$Default.make(TypeWriter.java:2224)
at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase$UsingTypeWriter.make(DynamicType.java:4050)
at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase.make(DynamicType.java:3734)
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.transform(InlineBytecodeGenerator.java:402)
at java.instrument/java.lang.instrument.ClassFileTransformer.transform(ClassFileTransformer.java:244)
at java.instrument/sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at java.instrument/sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:610)
at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:225)
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.triggerRetransformation(InlineBytecodeGenerator.java:281)

Tim van der Lippe

unread,
Jan 8, 2025, 8:05:38 AMJan 8
to moc...@googlegroups.com

Mockito depends on the latest version of ByteBuddy which supports JDK 21. Make sure that in your dependency resolution, the version of ByteBuddy transitively resolves to the correct version. Most likely a different dependency pulls in an old version.


--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/mockito/63b79b58-a817-4a4f-90d7-1f7e70daebdcn%40googlegroups.com.

Pablo

unread,
Jan 8, 2025, 9:14:14 AMJan 8
to moc...@googlegroups.com
I was using mockito-inline 5.2.0, this has as dependency mockito-core 5.2.0. Mockito-Inline has not been updated with the new versions of mockito-core

--
You received this message because you are subscribed to a topic in the Google Groups "mockito" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mockito/psRyztIa5mY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mockito+u...@googlegroups.com.

Pablo

unread,
Jan 8, 2025, 9:14:14 AMJan 8
to mockito
this is the dependencies of mockito-core 5.2.0, as you can see the version of bytebuddy is hardcoded to 1.14.1

<issueManagement>
<system>GitHub issues</system>
<url>https://github.com/mockito/mockito/issues</url>
</issueManagement>
<ciManagement>
<system>GH Actions</system>
<url>https://github.com/mockito/mockito/actions</url>
</ciManagement>
<dependencies>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.14.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.14.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>3.3</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>

Tim van der Lippe

unread,
Jan 8, 2025, 9:21:53 AMJan 8
to moc...@googlegroups.com

We stopped publishing mockito-inline as a separate artifact as the inline mockmaker is the default since Mockito 5. You can now use mockito-core with its latest version. For more info see https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#0.2


You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/mockito/CAJpkR6fdEnpK8GE%3DNmYiiPy1iXBPP3ampxTKbKMHhGLQWdBGGg%40mail.gmail.com.

Celi Sun

unread,
Mar 20, 2025, 1:52:11 AMMar 20
to mockito
Hi Pablo,

I have the same issue as you when starting to use jdk21 for my bazel repo. Wonder how did you end up resolving the error?

    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>5.4.0</version>
      <licenses>
        <license>
          <name>The MIT License</name>
          <url>https://github.com/mockito/mockito/blob/main/LICENSE</url>
          <distribution>repo</distribution>
          <file>the mit license - license.html</file>
        </license>
      </licenses>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-inline</artifactId>
      <version>5.2.0</version>
      <licenses>
        <license>
          <name>The MIT License</name>
          <url>https://github.com/mockito/mockito/blob/main/LICENSE</url>
          <distribution>repo</distribution>
          <file>the mit license - license.html</file>
        </license>
      </licenses>
    </dependency>
    <dependency>

Celi Sun

unread,
Mar 20, 2025, 1:52:17 AMMar 20
to mockito
Also, changing byte-buddy-agent and byte-buddy to 1.41.3 doesn't help https://team-review.git.corp.google.com/c/adm-cloud-git-owners/scp/+/2466209/4/licenses/licenses.xml

this is my error message from test

2) verify_success(com.google.crypto.tink.integration.awskmsv2.AwsKmsPublicKeyVerifyTest) 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:84) at jdk.proxy2/jdk.proxy2.$Proxy14.isTypeMockable(Unknown Source) at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:78) at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22) at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:274) at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:235) at org.mockito.internal.MockitoCore.mock(MockitoCore.java:86) at org.mockito.Mockito.mock(Mockito.java:2101) at org.mockito.internal.configuration.MockAnnotationProcessor.processAnnotationForMock(MockAnnotationProcessor.java:79) at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:28) at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:25) at org.mockito.internal.configuration.IndependentAnnotationEngine.createMockFor(IndependentAnnotationEngine.java:44) at org.mockito.internal.configuration.IndependentAnnotationEngine.process(IndependentAnnotationEngine.java:72) at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:62) at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:47) at org.mockito.MockitoAnnotations.openMocks(MockitoAnnotations.java:81) at org.mockito.internal.framework.DefaultMockitoSession.<init>(DefaultMockitoSession.java:43) at org.mockito.internal.session.DefaultMockitoSessionBuilder.startMocking(DefaultMockitoSessionBuilder.java:83) at org.mockito.internal.junit.JUnitSessionStore$1.evaluate(JUnitSessionStore.java:38) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at com.google.testing.junit.runner.internal.junit4.CancellableRequestFactory$CancellableRunner.run(CancellableRequestFactory.java:108) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at com.google.testing.junit.runner.junit4.JUnit4Runner.run(JUnit4Runner.java:116) at com.google.testing.junit.runner.BazelTestRunner.runTestsInSuite(BazelTestRunner.java:145) at com.google.testing.junit.runner.BazelTestRunner.main(BazelTestRunner.java:76) 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:104) at org.mockito.internal.configuration.plugins.DefaultMockitoPlugins.getDefaultPlugin(DefaultMockitoPlugins.java:78) at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:75) at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:50) at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:27) at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:22) at org.mockito.junit.MockitoJUnit.rule(MockitoJUnit.java:34) at com.google.crypto.tink.integration.awskmsv2.AwsKmsPublicKeyVerifyTest.<init>(AwsKmsPublicKeyVerifyTest.java:34) at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486) at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:250) at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:260) at org.junit.runners.BlockJUnit4ClassRunner$2.runReflectiveCall(BlockJUnit4ClassRunner.java:309) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:306) ... 17 more Caused by: java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:74) 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:102) ... 32 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 : 21 JVM vendor name : Azul Systems, Inc. JVM vendor version : 21.0.4+7-LTS JVM name : OpenJDK 64-Bit Server VM JVM version : 21.0.4+7-LTS JVM info : mixed mode, sharing OS name : Linux OS version : 6.6.72+ at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.<init>(InlineDelegateByteBuddyMockMaker.java:244) at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<init>(InlineByteBuddyMockMaker.java:23) at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62) ... 35 more Caused by: java.lang.IllegalStateException: The Byte Buddy agent is not initialized or unavailable at net.bytebuddy.agent.ByteBuddyAgent.getInstrumentation(ByteBuddyAgent.java:230) at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:617) at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:568) at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:545) at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.<clinit>(InlineDelegateByteBuddyMockMaker.java:115) ... 37 more FAILURES!!!

Celi Sun

unread,
Mar 20, 2025, 1:52:24 AMMar 20
to mockito
Reply all
Reply to author
Forward
0 new messages