mockito icon indicating copy to clipboard operation
mockito copied to clipboard

Tests failures after adding mockito-inline on Java 21

Open MikhailNavitski opened this issue 10 months ago • 10 comments

Hi,

It appears that Mockito cannot mock several classes after adding mockito-inline dependency to the project

Mockito cannot mock this class: class org.apache.http.impl.client.CloseableHttpClient.

If you're not sure why you're getting this error, please open an issue on GitHub.

Java : 21 JVM vendor name : Eclipse Adoptium JVM vendor version : 21.0.2+13-LTS JVM name : OpenJDK 64-Bit Server VM JVM version : 21.0.2+13-LTS JVM info : mixed mode, sharing OS name : Windows Server 2022 OS version : 10.0

You are seeing this disclaimer because Mockito is configured to create inlined mocks. You can learn about inline mocks and their limitations under item #39 of the Mockito class javadoc.

Underlying exception : org.mockito.exceptions.base.MockitoException: Could not modify all classes [interface org.apache.http.client.HttpClient, interface java.lang.AutoCloseable, class org.apache.http.impl.client.CloseableHttpClient, class java.lang.Object, interface java.io.Closeable] at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:50) at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:100) at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:107) at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:41) at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163) Caused by: org.mockito.exceptions.base.MockitoException: Could not modify all classes [interface org.apache.http.client.HttpClient, interface java.lang.AutoCloseable, class org.apache.http.impl.client.CloseableHttpClient, class java.lang.Object, interface java.io.Closeable] at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:168) at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:399) at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:190) at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:410) ... 5 more Caused by: java.lang.IllegalStateException: Byte Buddy could not instrument all classes within the mock's type hierarchy

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

The Java project version - 8

Mockito dependencies:

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>4.11.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>4.11.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <version>4.11.0</version>
            <scope>test</scope>
        </dependency>

The code was successfully run on JDK 11,17

MikhailNavitski avatar Apr 27 '24 14:04 MikhailNavitski

mockito-inline should be updated with a new dependency.

maxim5 avatar Jun 07 '24 09:06 maxim5

will there be any update for mockito-inline to support JDK 21 in the future?

maryantocinn avatar Jun 20 '24 07:06 maryantocinn

I'm also facing this issue. Would be nice to have this update soon.

viniciusrd10 avatar Jul 26 '24 16:07 viniciusrd10

Is there a timeline by which we can expect the update to be released?

RaghavGoyal avatar Jul 30 '24 12:07 RaghavGoyal

hi Raghav, are u upgrading to java 21? I also need mockito inline

kanikajindal101 avatar Jul 31 '24 08:07 kanikajindal101

so mockito inline will not work in java 21...my test cases are failing

kanikajindal101 avatar Jul 31 '24 08:07 kanikajindal101

@kanikajindal101 Yes, I recently upgraded to JDK 21 and that's when this issue is encountered.

RaghavGoyal avatar Jul 31 '24 08:07 RaghavGoyal

For me what worked was to add a byte-butty version that supports Java 21. It is 1.14.17.

   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>net.bytebuddy</groupId>
               <artifactId>byte-buddy</artifactId>
               <version>${byte-buddy.version}</version>
           </dependency>
       </dependencies>
   </dependencyManagement>

viniciusrd10 avatar Jul 31 '24 09:07 viniciusrd10

@viniciusrd10 are u not getting this warning ?

A Java agent has been loaded dynamically byte-buddy-agent-1.14.10.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 OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended INFO [2024-08-01 09:08:59,754] com.assaabloy.msf.common.messaging.client.RabbitMQMessageService: Consumer amq.ctag-cH4fX34_L3gNvPeVqqTu

kanikajindal101 avatar Aug 01 '24 09:08 kanikajindal101

For me what worked was to add a byte-butty version that supports Java 21. It is 1.14.17.

   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>net.bytebuddy</groupId>
               <artifactId>byte-buddy</artifactId>
               <version>${byte-buddy.version}</version>
           </dependency>
       </dependencies>
   </dependencyManagement>

It helped me too, thanks!

user873475320 avatar Aug 26 '24 22:08 user873475320