Repository: mockito/mockito
Issue: Stackoverflow error when upgrading to v3.5.2

I tried to upgrade from Mockito v3.4.6 to v3.5.2 and I'm getting a stackoverflow error (same error with v3.5.0) when running some tests (not all of them, just a few ; even in the same class : some tests pass and others don't).

Stacktrace :
```
java.lang.StackOverflowError
	at java.base/java.lang.ThreadLocal.get(ThreadLocal.java:163)
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.lambda$new$3(InlineByteBuddyMockMaker.java:260)
	at org.mockito.internal.creation.bytebuddy.MockMethodAdvice.handleConstruction(MockMethodAdvice.java:176)
	at org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher.handleConstruction(MockMethodDispatcher.java:53)
	at java.base/java.lang.Throwable.<init>(Throwable.java)
	at java.base/java.lang.Exception.<init>(Exception.java:66)
	at java.base/java.lang.RuntimeException.<init>(RuntimeException.java:62)
	at java.base/java.lang.ClassCastException.<init>(ClassCastException.java:58)
	at java.base/java.lang.Throwable.<init>(Throwable.java)
	at java.base/java.lang.Exception.<init>(Exception.java:66)
	at java.base/java.lang.RuntimeException.<init>(RuntimeException.java:62)
	at java.base/java.lang.ClassCastException.<init>(ClassCastException.java:58)
	at java.base/java.lang.Throwable.<init>(Throwable.java)
	at java.base/java.lang.Exception.<init>(Exception.java:66)
        ...
```

I didn't succeed in reproduce the error in a small project, but I'll spend more time to try and post the sources here when/if I have a reproducible case.

Environment :
- Mockito v3.5.2
- JUnit v5.6.2
- Java :
> openjdk version "11.0.8" 2020-07-14
> OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu118.04.1)
> OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu118.04.1, mixed mode, sharing)
- Maven : v3.6.0 (tests are run by the maven-surefire plugin)

Issue comments:
I just discovered that the error happens when I launch all my unit tests in the Maven module, but it's fine if I run the test or the test class only.

```sh
$ cd myproject/my-mvn-module-1
$ mvn test
#
# [ERROR]   MyBeanTest.test1 » StackOverflow
#
$ mvn -Dtest=MyBeanTest test
# 
# Tests run: 7, Failures: 0, Errors: 0, Skipped: 0
#
$ mvn -Dtest=MyBeanTest#test1 test
# 
# Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
#
```

Maven configuration (in the root pom.xml) :
```xml
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <skipTests>${skip.unit.tests}</skipTests>
        <includes>
            <include>**/*Test.java</include>
        </includes>
        <argLine>-Dnet.bytebuddy.experimental=true @{argLine} -Dfile.encoding=${project.reporting.outputEncoding}</argLine>
        <trimStackTrace>false</trimStackTrace>
    </configuration>
</plugin>
```
Thanks for reporting. Can you supply an example test case? It looks like an exception is supposed to be thrown but the constructor mocking intercepts its creation where something causes another exception which is again passed to the handler.

Do you mock exceptions somewhere?

Also, could you share the bottom of the stack trace which is where the circularity is introduced.
Seeing the same thing myself, although I don't have a stable reproduction scenario. The same was observed with `3.5.0`
Could you try the latest release to see if the problem still occurs?
Sorry I was not clear originally, I saw the problem at first on `3.5.0` but still see it with `3.5.2`.
@raphw 

Yes I mock some exceptions in other test classes but the ones who ends with StackoverflowError. I will take some time today or next week to try to reproduce the bug in a small project.

The full stacktrace :
```
java.lang.StackOverflowError
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.lambda$new$3(InlineByteBuddyMockMaker.java)
	at org.mockito.internal.creation.bytebuddy.MockMethodAdvice.handleConstruction(MockMethodAdvice.java:176)
	at org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher.handleConstruction(MockMethodDispatcher.java:53)
	at java.base/java.lang.Throwable.<init>(Throwable.java)
	at java.base/java.lang.Exception.<init>(Exception.java:66)
	at java.base/java.lang.RuntimeException.<init>(RuntimeException.java:62)
	at java.base/java.lang.ClassCastException.<init>(ClassCastException.java:58)
	at java.base/java.lang.Throwable.<init>(Throwable.java)
	at java.base/java.lang.Exception.<init>(Exception.java:66)
	at java.base/java.lang.RuntimeException.<init>(RuntimeException.java:62)
	at java.base/java.lang.ClassCastException.<init>(ClassCastException.java:58)
	at java.base/java.lang.Throwable.<init>(Throwable.java)
	at java.base/java.lang.Exception.<init>(Exception.java:66)
	at java.base/java.lang.RuntimeException.<init>(RuntimeException.java:62)
	at java.base/java.lang.ClassCastException.<init>(ClassCastException.java:58)
	/* [...] */
	at java.base/java.lang.Throwable.<init>(Throwable.java)
	at java.base/java.lang.Exception.<init>(Exception.java:66)
	at java.base/java.lang.RuntimeException.<init>(RuntimeException.java:62)
	at java.base/java.lang.ClassCastException.<init>(ClassCastException.java:58)
	at java.base/java.lang.Throwable.<init>(Throwable.java)
	at java.base/java.lang.Exception.<init>(Exception.java:66)
	at java.base/java.lang.RuntimeException.<init>(RuntimeException.java:62)
	at java.base/java.lang.ClassCastException.<init>(ClassCastException.java:58)
	at java.base/java.lang.Throwable.<init>(Throwable.java)
	/* end of stacktrace */
```
This can't be the full stack trace since it needs to origin somewhere. Maybe it's cut off in the print?

I think I know the problem and tried to fix it. Unfortunately, we have some release issues but you could build *release/3.x* locally and try if the problem still occurs.
I'm seeing the same error when trying to go from 3.4.6 to 3.5.0.
```
java.lang.StackOverflowError
	at org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher.isConstructorMock(MockMethodDispatcher.java:57)
	at java.base/java.lang.Exception.<init>(Exception.java)
	at java.base/java.lang.RuntimeException.<init>(RuntimeException.java:62)
	at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:34)
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.lambda$new$3(InlineByteBuddyMockMaker.java:273)
	at org.mockito.internal.creation.bytebuddy.MockMethodAdvice.handleConstruction(MockMethodAdvice.java:176)
	at org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher.handleConstruction(MockMethodDispatcher.java:69)
	at java.base/java.lang.Throwable.<init>(Throwable.java)
	at java.base/java.lang.Exception.<init>(Exception.java)
	at java.base/java.lang.RuntimeException.<init>(RuntimeException.java:62)
	at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:34)
... 
```
seems to be related to using a @Spy in the test class, when I change the @Spy to an @Mock the error goes away.  What seems odd in my case is everything is ok until there are more than 15 tests in the class.  15 tests will pass and any additional tests will fail.

The message of the first exception is `Unexpected spy for java.util.AbstractMap on instance of java.util.concurrent.ConcurrentHashMap`   
@raphw Same issue with v3.5.5.

Stacktrace :
```
java.lang.StackOverflowError
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.lambda$new$3(InlineByteBuddyMockMaker.java:273)
	at org.mockito.internal.creation.bytebuddy.MockMethodAdvice.handleConstruction(MockMethodAdvice.java:176)
	at org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher.handleConstruction(MockMethodDispatcher.java:69)
	at java.base/java.lang.Throwable.<init>(Throwable.java)
	at java.base/java.lang.Exception.<init>(Exception.java:66)
	at java.base/java.lang.RuntimeException.<init>(RuntimeException.java:62)
	at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:34)
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.lambda$new$3(InlineByteBuddyMockMaker.java:273)
	at org.mockito.internal.creation.bytebuddy.MockMethodAdvice.handleConstruction(MockMethodAdvice.java:176)
	at org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher.handleConstruction(MockMethodDispatcher.java:69)
	at java.base/java.lang.Throwable.<init>(Throwable.java)
	at java.base/java.lang.Exception.<init>(Exception.java:66)
	at java.base/java.lang.RuntimeException.<init>(RuntimeException.java:62)
	at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:34)
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.lambda$new$3(InlineByteBuddyMockMaker.java:273)
	at org.mockito.internal.creation.bytebuddy.MockMethodAdvice.handleConstruction(MockMethodAdvice.java:176)
	at org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher.handleConstruction(MockMethodDispatcher.java:69)
        [...]
	at java.base/java.lang.Throwable.<init>(Throwable.java)
	at java.base/java.lang.Exception.<init>(Exception.java:66)
	at java.base/java.lang.RuntimeException.<init>(RuntimeException.java:62)
	at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:34)
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.lambda$new$3(InlineByteBuddyMockMaker.java:273)
	at org.mockito.internal.creation.bytebuddy.MockMethodAdvice.handleConstruction(MockMethodAdvice.java:176)
	at org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher.handleConstruction(MockMethodDispatcher.java:69)
	at java.base/java.lang.Throwable.<init>(Throwable.java)
	at java.base/java.lang.Exception.<init>(Exception.java:66)
	at java.base/java.lang.RuntimeException.<init>(RuntimeException.java:62)
	at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:34)
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.lambda$new$3(InlineByteBuddyMockMaker.java:273)
	at org.mockito.internal.creation.bytebuddy.MockMethodAdvice.handleConstruction(MockMethodAdvice.java:176)
	at org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher.handleConstruction(MockMethodDispatcher.java:69)
	at java.base/java.lang.Throwable.<init>(Throwable.java)
	at java.base/java.lang.Exception.<init>(Exception.java:66)
	at java.base/java.lang.RuntimeException.<init>(RuntimeException.java:62)
	at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:34)
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.lambda$new$3(InlineByteBuddyMockMaker.java:273)
	at org.mockito.internal.creation.bytebuddy.MockMethodAdvice.handleConstruction(MockMethodAdvice.java:176)
```
@jdussouillez That gets us one step closer but I needed to add an exception for when the error is thrown. Could you build the *constructor-dispatch* branch locally and see what exception you get in your setup?
@raphw I installed v3.5.6 from the *constructor-dispatch* branch : no more errors, all the tests were executed and passed. So I guess commit https://github.com/mockito/mockito/commit/eaa12bfc018ed00fcd35588dc900d657a9740e32 fixes the issue.

Feel free to ask if you want me to do more tests.
That's strange since the exception indicates a scenario that should not occur. I merely fixed the recursion.

Can you set a breakppint to see why this happens? What instance is created during spy construction? 
@raphw I didn't succeed in setting breakpoints but I added some logs (before [this line](https://github.com/mockito/mockito/blob/constructor-dispatch/src/main/java/org/mockito/internal/creation/bytebuddy/InlineByteBuddyMockMaker.java#L268)).

Patch : 
```diff
diff --git a/src/main/java/org/mockito/internal/creation/bytebuddy/InlineByteBuddyMockMaker.java b/src/main/java/org/mockito/internal/creation/bytebuddy/InlineByteBuddyMockMaker.java
index 96477f477..6b37ea09e 100644
--- a/src/main/java/org/mockito/internal/creation/bytebuddy/InlineByteBuddyMockMaker.java
+++ b/src/main/java/org/mockito/internal/creation/bytebuddy/InlineByteBuddyMockMaker.java
@@ -265,6 +265,9 @@ public class InlineByteBuddyMockMaker
                         } else if (type.isInstance(spy)) {
                             return spy;
                         } else {
+                            System.out.println("type = " + type.getName());
+                            System.out.println("object = " + object);
+                            System.out.println("spy = " + spy.getClass().getName());
                             isSuspended.set(true);
                             try {
                                 // Unexpected construction of non-spied object
```

Logs (type is always `java.util.AbstractMap`):
```
type = java.util.AbstractMap
object = {}
spy = com.myproject.bean.MyBean1

type = java.util.AbstractMap
object = {}
spy = com.myproject.bean.MyBean2

type = java.util.AbstractMap
object = {}
spy = com.myproject.bean.MyBean3
[...]
```
Could you also add a stack trace at the same spot? (`Thread.dump()`)
Code snippet to dump : 
```java
try {
    Thread.dumpStack();
} catch (Throwable t) {
    System.out.println("error while dumping stack: " + t.getMessage());
}
```


1. Logs using [`Thread.dumpStack()`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Thread.html#dumpStack()) **after** `isSuspended.set(true);`

```
type = java.util.AbstractMap
object = {}
spy = com.myproject.bean.MyBeanTest$TestClass
<dump>
java.lang.Exception: Stack trace
	at java.base/java.lang.Thread.dumpStack(Thread.java:1388)
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.lambda$new$3(InlineByteBuddyMockMaker.java:272)
	at org.mockito.internal.creation.bytebuddy.MockMethodAdvice.handleConstruction(MockMethodAdvice.java:176)
	at org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher.handleConstruction(MockMethodDispatcher.java:69)
	at java.base/java.util.AbstractMap.<init>(AbstractMap.java)
	at java.base/java.util.concurrent.ConcurrentHashMap.<init>(ConcurrentHashMap.java:828)
	at java.base/java.lang.ClassLoader.<init>(ClassLoader.java:333)
	at java.base/java.lang.ClassLoader.<init>(ClassLoader.java:457)
	at java.base/jdk.internal.reflect.DelegatingClassLoader.<init>(ClassDefiner.java:72)
	at java.base/jdk.internal.reflect.ClassDefiner$1.run(ClassDefiner.java:60)
	at java.base/jdk.internal.reflect.ClassDefiner$1.run(ClassDefiner.java:58)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/jdk.internal.reflect.ClassDefiner.defineClass(ClassDefiner.java:57)
	at java.base/jdk.internal.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:400)
	at java.base/jdk.internal.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:394)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/jdk.internal.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:393)
	at java.base/jdk.internal.reflect.MethodAccessorGenerator.generateConstructor(MethodAccessorGenerator.java:92)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:55)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at org.mockito.internal.util.reflection.ReflectionMemberAccessor.lambda$newInstance$0(ReflectionMemberAccessor.java:26)
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.lambda$newInstance$4(InlineByteBuddyMockMaker.java:631)
	at org.mockito.internal.util.reflection.ReflectionMemberAccessor.newInstance(ReflectionMemberAccessor.java:26)
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.newInstance(InlineByteBuddyMockMaker.java:626)
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.doCreateMock(InlineByteBuddyMockMaker.java:373)
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.createSpy(InlineByteBuddyMockMaker.java:349)
	at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:44)
	at org.mockito.internal.MockitoCore.mock(MockitoCore.java:61)
	at org.mockito.Mockito.spy(Mockito.java:2033)
	at com.myproject.bean.MyBeanTest.test1(MyBeanTest.java:274)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686)
	at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
	at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
	at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:212)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:208)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
	at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
	at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
	at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invokeAllTests(JUnitPlatformProvider.java:150)
	at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:124)
	at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384)
	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345)
	at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126)
	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)
</dump>
```

2. Logs using [`Thread.dumpStack()`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Thread.html#dumpStack()) **before** `isSuspended.set(true);`

```
type = java.util.AbstractMap
object = {}
spy = com.myproject.library.LibraryBean1
<dump>
type = java.lang.Throwable
object = java.lang.Exception
spy = com.myproject.library.LibraryBean1
<dump>
type = java.lang.Throwable
object = java.lang.Exception
spy = com.myproject.library.LibraryBean1

[...]

error while dumping stack: Unexpected spy for java.lang.Throwable on instance of java.lang.Exception
</dump>
error while dumping stack: Unexpected spy for java.lang.Throwable on instance of java.lang.Exception
</dump>
error while dumping stack: Unexpected spy for java.lang.Throwable on instance of java.lang.Exception
</dump>
error while dumping stack: Unexpected spy for java.lang.Throwable on instance of java.lang.Exception
</dump>
error while dumping stack: Unexpected spy for java.lang.Throwable on instance of java.lang.Exception
</dump>
```
I thought it would be related to class loading.

I am curious however how you end uo with the reflection member accessor. It should be the instrumentation based one. Are you using the latest build? 
> Are you using the latest build?

I pulled the *constructor-dispatch* branch this morning before giving you the `Thread.dumpStack()` output. I just switched to branch *release/3.x* (3.5.6, next 3.5.7), applied my logs and run my tests again : same results.

Strange, I'll investigate. Could you make sure that you don't have a stale version? You are using *mockito-inline* or are you configuring the mock maker manually? 
> Could you make sure that you don't have a stale version?

I already checked using `mvn dependency:tree` but it says I'm using 3.5.7 (I'm using the *release/3.x* branch).
I will try to copy my project, use v3.5.2 and remove code step by step until I can reproduce the bug with only a few files so I can provide you a reproducible example.

----

> You are using mockito-inline or are you configuring the mock maker manually?

I'm using mockito-core :

```xml
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>3.5.7</version>
    <scope>test</scope>
</dependency>
```

And I do not configure anything manually. I just have the `src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker` config file which contains
```
mock-maker-inline
```

That explains it. You can drop the config and just use the inline artifact which defines the nock maker for you. The member accessor is another plugin which requires manual activation using a similar file if you don't use the other artifact. 
> You can drop the config and just use the inline artifact which defines the nock maker for you.

I used mockito-inline v3.5.6, all tests passed but 3. The 3 that fails are new ones (not the ones who failed before) and none of them use Mockito (but all of them are on enums, apparently `MyEnum.values()` returns an array with null values inside). But they're executed correctly when running them manually, class by class ([like I did before](https://github.com/mockito/mockito/issues/2011#issuecomment-677610343))

Do you know how can I generate the inline JAR from the sources ? I can't provide you the logs I added in the source with mockito-inline because the JAR is not generated by `./gradlew -x check clean build`.
@raphw Thank you for the fix and the time spent on this. It works great !

Pull request review comments:

Patch that resolved the issue:
diff --git a/src/main/java/org/mockito/internal/creation/bytebuddy/InlineBytecodeGenerator.java b/src/main/java/org/mockito/internal/creation/bytebuddy/InlineBytecodeGenerator.java
index 409f58201c..067dea7a04 100644
--- a/src/main/java/org/mockito/internal/creation/bytebuddy/InlineBytecodeGenerator.java
+++ b/src/main/java/org/mockito/internal/creation/bytebuddy/InlineBytecodeGenerator.java
@@ -206,6 +206,7 @@ public <T> Class<? extends T> mockClass(MockFeatures<T> features) {
         Set<Class<?>> types = new HashSet<>();
         types.add(features.mockedType);
         types.addAll(features.interfaces);
+
         synchronized (this) {
             triggerRetransformation(types, false);
         }
@@ -223,17 +224,26 @@ public synchronized void mockClassConstruction(Class<?> type) {
         triggerRetransformation(Collections.singleton(type), false);
     }
 
+    private static void assureInitialization(Class<?> type) {
+        try {
+            Class.forName(type.getName(), true, type.getClassLoader());
+        } catch (Throwable ignore) {
+        }
+    }
+
     private <T> void triggerRetransformation(Set<Class<?>> types, boolean flat) {
         Set<Class<?>> targets = new HashSet<Class<?>>();
 
         for (Class<?> type : types) {
             if (flat) {
                 if (!mocked.contains(type) && flatMocked.add(type)) {
+                    assureInitialization(type);
                     targets.add(type);
                 }
             } else {
                 do {
                     if (mocked.add(type)) {
+                        assureInitialization(type);
                         if (!flatMocked.remove(type)) {
                             targets.add(type);
                         }


Test patch:
diff --git a/subprojects/inline/src/test/java/org/mockitoinline/InitializationTest.java b/subprojects/inline/src/test/java/org/mockitoinline/InitializationTest.java
new file mode 100644
index 0000000000..c4f62f400f
--- /dev/null
+++ b/subprojects/inline/src/test/java/org/mockitoinline/InitializationTest.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2020 Mockito contributors
+ * This program is made available under the terms of the MIT License.
+ */
+package org.mockitoinline;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import static junit.framework.TestCase.assertEquals;
+
+public class InitializationTest {
+
+    @Test
+    public void assure_initialization_prior_to_instrumentation() {
+        @SuppressWarnings("unused")
+        SampleEnum mock = Mockito.mock(SampleEnum.class);
+        SampleEnum[] values = SampleEnum.values();
+        assertEquals("VALUE", values[0].name());
+    }
+
+    public enum SampleEnum {
+        VALUE
+    }
+}
diff --git a/subprojects/inline/src/test/java/org/mockitoinline/PluginTest.java b/subprojects/inline/src/test/java/org/mockitoinline/PluginTest.java
index 19526db2a9..16e2da2915 100644
--- a/subprojects/inline/src/test/java/org/mockitoinline/PluginTest.java
+++ b/subprojects/inline/src/test/java/org/mockitoinline/PluginTest.java
@@ -7,14 +7,20 @@
 import org.junit.Test;
 import org.mockito.internal.configuration.plugins.Plugins;
 import org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker;
+import org.mockito.internal.util.reflection.ModuleMemberAccessor;
 
 import static org.junit.Assert.*;
 
 public class PluginTest {
 
     @Test
-    public void plugin_type_should_be_inline() throws Exception {
+    public void mock_maker_should_be_inline() throws Exception {
         assertTrue(Plugins.getMockMaker() instanceof InlineByteBuddyMockMaker);
     }
 
+    @Test
+    public void member_accessor_should_be_module() throws Exception {
+        assertTrue(Plugins.getMemberAccessor() instanceof ModuleMemberAccessor);
+    }
+
 }