@@ -200,6 +200,10 @@ public void run() {
200
200
e .addSuppressed (ex );
201
201
}
202
202
throw e ;
203
+ } finally {
204
+ if (originalCl != null ) {
205
+ Thread .currentThread ().setContextClassLoader (originalCl );
206
+ }
203
207
}
204
208
}
205
209
@@ -237,10 +241,15 @@ public void afterEach(ExtensionContext context) throws Exception {
237
241
afterEachCallback .getClass ().getMethod ("afterEach" , Object .class ).invoke (afterEachCallback , actualTestInstance );
238
242
}
239
243
boolean nativeImageTest = isNativeTest (context );
240
- runningQuarkusApplication .getClassLoader ().loadClass (RestAssuredURLManager .class .getName ())
241
- .getDeclaredMethod ("clearURL" ).invoke (null );
242
- runningQuarkusApplication .getClassLoader ().loadClass (TestScopeManager .class .getName ())
243
- .getDeclaredMethod ("tearDown" , boolean .class ).invoke (null , nativeImageTest );
244
+ ClassLoader original = setCCL (runningQuarkusApplication .getClassLoader ());
245
+ try {
246
+ runningQuarkusApplication .getClassLoader ().loadClass (RestAssuredURLManager .class .getName ())
247
+ .getDeclaredMethod ("clearURL" ).invoke (null );
248
+ runningQuarkusApplication .getClassLoader ().loadClass (TestScopeManager .class .getName ())
249
+ .getDeclaredMethod ("tearDown" , boolean .class ).invoke (null , nativeImageTest );
250
+ } finally {
251
+ setCCL (original );
252
+ }
244
253
}
245
254
}
246
255
@@ -254,17 +263,22 @@ public void beforeEach(ExtensionContext context) throws Exception {
254
263
return ;
255
264
}
256
265
if (!failedBoot ) {
257
- pushMockContext ();
258
- for (Object beforeEachCallback : beforeEachCallbacks ) {
259
- beforeEachCallback .getClass ().getMethod ("beforeEach" , Object .class ).invoke (beforeEachCallback ,
260
- actualTestInstance );
261
- }
262
- boolean nativeImageTest = isNativeTest (context );
263
- if (runningQuarkusApplication != null ) {
264
- runningQuarkusApplication .getClassLoader ().loadClass (RestAssuredURLManager .class .getName ())
265
- .getDeclaredMethod ("setURL" , boolean .class ).invoke (null , false );
266
- runningQuarkusApplication .getClassLoader ().loadClass (TestScopeManager .class .getName ())
267
- .getDeclaredMethod ("setup" , boolean .class ).invoke (null , nativeImageTest );
266
+ ClassLoader original = setCCL (runningQuarkusApplication .getClassLoader ());
267
+ try {
268
+ pushMockContext ();
269
+ for (Object beforeEachCallback : beforeEachCallbacks ) {
270
+ beforeEachCallback .getClass ().getMethod ("beforeEach" , Object .class ).invoke (beforeEachCallback ,
271
+ actualTestInstance );
272
+ }
273
+ boolean nativeImageTest = isNativeTest (context );
274
+ if (runningQuarkusApplication != null ) {
275
+ runningQuarkusApplication .getClassLoader ().loadClass (RestAssuredURLManager .class .getName ())
276
+ .getDeclaredMethod ("setURL" , boolean .class ).invoke (null , false );
277
+ runningQuarkusApplication .getClassLoader ().loadClass (TestScopeManager .class .getName ())
278
+ .getDeclaredMethod ("setup" , boolean .class ).invoke (null , nativeImageTest );
279
+ }
280
+ } finally {
281
+ setCCL (original );
268
282
}
269
283
} else {
270
284
if (firstException != null ) {
@@ -310,7 +324,6 @@ public void beforeAll(ExtensionContext context) throws Exception {
310
324
ensureStarted (context );
311
325
if (runningQuarkusApplication != null ) {
312
326
pushMockContext ();
313
- setCCL (runningQuarkusApplication .getClassLoader ());
314
327
}
315
328
}
316
329
@@ -380,11 +393,15 @@ public <T> T interceptTestClassConstructor(Invocation<T> invocation,
380
393
// We do this here as well, because when @TestInstance(Lifecycle.PER_CLASS) is used on a class,
381
394
// interceptTestClassConstructor is called before beforeAll, meaning that the TCCL will not be set correctly
382
395
// (for any test other than the first) unless this is done
396
+ old = null ;
383
397
if (runningQuarkusApplication != null ) {
384
- setCCL (runningQuarkusApplication .getClassLoader ());
398
+ old = setCCL (runningQuarkusApplication .getClassLoader ());
385
399
}
386
400
387
401
initTestState (extensionContext , state );
402
+ if (old != null ) {
403
+ setCCL (old );
404
+ }
388
405
return result ;
389
406
}
390
407
@@ -477,10 +494,10 @@ private Object runExtensionMethod(ReflectiveInvocationContext<Method> invocation
477
494
throws Throwable {
478
495
Method newMethod = null ;
479
496
497
+ ClassLoader old = setCCL (runningQuarkusApplication .getClassLoader ());
480
498
try {
481
499
Class <?> c = Class .forName (extensionContext .getRequiredTestClass ().getName (), true ,
482
500
Thread .currentThread ().getContextClassLoader ());
483
- ;
484
501
while (c != Object .class ) {
485
502
if (c .getName ().equals (invocationContext .getExecutable ().getDeclaringClass ().getName ())) {
486
503
try {
@@ -522,6 +539,8 @@ private Object runExtensionMethod(ReflectiveInvocationContext<Method> invocation
522
539
throw e .getCause ();
523
540
} catch (IllegalAccessException | ClassNotFoundException e ) {
524
541
throw new RuntimeException (e );
542
+ } finally {
543
+ setCCL (old );
525
544
}
526
545
}
527
546
0 commit comments