I am converting a code base from Mockito 1 to Mockito 3 and ran into a "ClassNotFoundException" problem: sometimes (every 8th execution or so) of a pretty simple unit test, the test fails with "ClassNotFoundException". I am using Java 1.8 for this. The stacktrace shows (abbreviated here):
Underlying exception : java.lang.IllegalArgumentException: Could not create type
at org.mockito.junit.jupiter.MockitoExtension.beforeEach(MockitoExtension.java:153)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeEachCallbacks$1(TestMethodTestDescriptor.java:159)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeMethodsOrCallbacksUntilExceptionOccurs$5(TestMethodTestDescriptor.java:195)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(TestMethodTestDescriptor.java:195)
Caused by: java.lang.IllegalArgumentException: Could not create type
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:155)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:366)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:175)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:377)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachingBytecodeGenerator.java:40)
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.mockClass(InlineBytecodeGenerator.java:216)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.lambda$mockClass$0(TypeCachingBytecodeGenerator.java:47)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:153)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:366)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:175)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:377)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachingBytecodeGenerator.java:40)
at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.createMockType(InlineDelegateByteBuddyMockMaker.java:391)
Caused by: java.lang.ExceptionInInitializerError
at java.util.concurrent.ConcurrentHashMap.fullAddCount(ConcurrentHashMap.java:2526)
at java.util.concurrent.ConcurrentHashMap.addCount(ConcurrentHashMap.java:2266)
at java.util.concurrent.ConcurrentHashMap.replaceNode(ConcurrentHashMap.java:1166)
at java.util.concurrent.ConcurrentHashMap.remove(ConcurrentHashMap.java:1097)
at org.mockito.internal.util.concurrent.WeakConcurrentMap.expungeStaleEntries(WeakConcurrentMap.java:139)
at org.mockito.internal.util.concurrent.WeakConcurrentMap$WithInlinedExpunction.containsKey(WeakConcurrentMap.java:272)
at org.mockito.internal.creation.bytebuddy.MockMethodAdvice.isMock(MockMethodAdvice.java:187)
Caused by: java.lang.NullPointerException
at java.util.concurrent.ThreadLocalRandom.getProbe(ThreadLocalRandom.java:980)
at java.util.concurrent.ConcurrentHashMap.fullAddCount(ConcurrentHashMap.java:2526)
at java.util.concurrent.ConcurrentHashMap.addCount(ConcurrentHashMap.java:2266)
at java.util.concurrent.ConcurrentHashMap.replaceNode(ConcurrentHashMap.java:1166)
at java.util.concurrent.ConcurrentHashMap.remove(ConcurrentHashMap.java:1097)
at org.mockito.internal.util.concurrent.WeakConcurrentMap.expungeStaleEntries(WeakConcurrentMap.java:139)
I found a few OpenJDK error reports about an internal circular dependency between ThreadLocalRandom and ConcurrentHashMap, but they seem to address a bug that was introduced in early releases of JDK9 (and then fixed sometime in 2016). However, this is the first time (in a pretty large and often used Java codebase) that I encountered this problem. Is this a known problem with Mockito 3 and Java 1.8?