In java version 13 I've been facing this issue around Mockito in my spring project, which failing test cases.

Exception Summary

  • Underlying exception : org.mockito.exceptions.base.MockitoException: Could not modify all classes [interface java.util.Map
  • Caused by: java.lang.IllegalStateException
  • Caused by: java.lang.IllegalArgumentException: Unsupported class file major version 57

Details

Java HotSpot(TM) 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: interface java.util.Map.

If you're not sure why you're getting this error, please report to the mailing list.

Java               : 13
JVM vendor name    : Oracle Corporation
JVM vendor version : 13.0.2+8
JVM name           : Java HotSpot(TM) 64-Bit Server VM
JVM version        : 13.0.2+8
JVM info           : mixed mode, sharing
OS name            : Mac OS X
OS version         : 10.15.6


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 java.util.Map]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: org.mockito.exceptions.base.MockitoException: Could not modify all classes [interface java.util.Map]
	at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:152)
	at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:365)
	at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:174)
	at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:376)
	... 68 more
Caused by: java.lang.IllegalStateException: 
Byte Buddy could not instrument all classes within the mock's type hierarchy


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:176)
	....
	at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:69)
	... 68 more
Caused by: java.lang.IllegalArgumentException: Unsupported class file major version 57
	at net.bytebuddy.jar.asm.ClassReader.<init>(ClassReader.java:184)
	at net.bytebuddy.jar.asm.ClassReader.<init>(ClassReader.java:166)
	at net.bytebuddy.jar.asm.ClassReader.<init>(ClassReader.java:152)
	at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.triggerRetransformation(InlineBytecodeGenerator.java:173)
	... 89 more

Solution

I was using an old Mockito version. Upgrading it to a new version solved the problem.