@@ -218,7 +218,7 @@ class InlineDelegateByteBuddyMockMaker
218
218
private final DetachedThreadLocal <Map <Class <?>, BiConsumer <Object , MockedConstruction .Context >>>
219
219
mockedConstruction = new DetachedThreadLocal <>(DetachedThreadLocal .Cleaner .MANUAL );
220
220
221
- private final ThreadLocal <Boolean > mockitoConstruction = ThreadLocal .withInitial (() -> false );
221
+ private final ThreadLocal <Class <?>> currentMocking = ThreadLocal .withInitial (() -> null );
222
222
223
223
private final ThreadLocal <Object > currentSpied = new ThreadLocal <>();
224
224
@@ -272,7 +272,9 @@ class InlineDelegateByteBuddyMockMaker
272
272
type -> {
273
273
if (isSuspended .get ()) {
274
274
return false ;
275
- } else if (mockitoConstruction .get () || currentConstruction .get () != null ) {
275
+ } else if ((currentMocking .get () != null
276
+ && type .isAssignableFrom (currentMocking .get ()))
277
+ || currentConstruction .get () != null ) {
276
278
return true ;
277
279
}
278
280
Map <Class <?>, ?> interceptors = mockedConstruction .get ();
@@ -290,7 +292,7 @@ class InlineDelegateByteBuddyMockMaker
290
292
};
291
293
ConstructionCallback onConstruction =
292
294
(type , object , arguments , parameterTypeNames ) -> {
293
- if (mockitoConstruction .get ()) {
295
+ if (currentMocking .get () != null ) {
294
296
Object spy = currentSpied .get ();
295
297
if (spy == null ) {
296
298
return null ;
@@ -647,11 +649,11 @@ public <T> T newInstance(Class<T> cls) throws InstantiationException {
647
649
accessor .newInstance (
648
650
selected ,
649
651
callback -> {
650
- mockitoConstruction .set (true );
652
+ currentMocking .set (cls );
651
653
try {
652
654
return callback .newInstance ();
653
655
} finally {
654
- mockitoConstruction . set ( false );
656
+ currentMocking . remove ( );
655
657
}
656
658
},
657
659
arguments );
0 commit comments