Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mockito cannot mock this class: class java.nio.ByteBuffer. in Java 21 #3202

Closed
xyraclius opened this issue Dec 16, 2023 · 1 comment
Closed

Comments

@xyraclius
Copy link

It works in Java 17, but it doesn't work in Java 21. I got this error messages:

OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class java.nio.ByteBuffer.

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

Java : 21
JVM vendor name : Red Hat, Inc.
JVM vendor version : 21.0.1+12-LTS
JVM name : OpenJDK 64-Bit Server VM
JVM version : 21.0.1+12-LTS
JVM info : mixed mode, sharing
OS name : Windows 10
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: Unsupported settings with this type 'java.nio.ByteBuffer'

at org.mockito.junit.jupiter.MockitoExtension.beforeEach(MockitoExtension.java:159)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Suppressed: java.lang.NullPointerException: Cannot invoke "java.util.Set.forEach(java.util.function.Consumer)" because the return value of "org.junit.jupiter.api.extension.ExtensionContext$Store.remove(Object, java.lang.Class)" is null
	at org.mockito.junit.jupiter.MockitoExtension.afterEach(MockitoExtension.java:190)
	... 2 more 

Caused by: org.mockito.exceptions.base.MockitoException: Unsupported settings with this type 'java.nio.ByteBuffer'
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)
... 3 more

the code:

import java.nio.ByteBuffer;

@Test
void bufferBody_Success() {
   var byteBuffer = mock(ByteBuffer.class);
}
@TimvdLippe
Copy link
Contributor

Unfortunately, ByteBuffer is one of the classes that Mockito relies on internally for its behavior. Stubbing ByteBuffer will therefore lead to undefined behavior. Additionally, it is advised not to mock classes you don't own: https://github.com/mockito/mockito/wiki/How-to-write-good-tests#dont-mock-a-type-you-dont-own We are working on improving the user experience by working on a DoNotMock feature to avoid mocking classes/methods that are known to crash Mockito internals (#1833). Therefore, I am closing this as "Infeasible". Apologies for the uninformative exception that is thrown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants