@@ -94,13 +94,8 @@ public class TestContextManager {
94
94
95
95
private final TestContext testContext ;
96
96
97
- private final ThreadLocal <TestContext > testContextHolder = new ThreadLocal <TestContext >() {
98
-
99
- @ Override
100
- protected TestContext initialValue () {
101
- return copyTestContext (TestContextManager .this .testContext );
102
- }
103
- };
97
+ private final ThreadLocal <TestContext > testContextHolder =
98
+ ThreadLocal .withInitial (() -> copyTestContext (TestContextManager .this .testContext ));
104
99
105
100
private final List <TestExecutionListener > testExecutionListeners = new ArrayList <>();
106
101
@@ -364,11 +359,13 @@ public void beforeTestExecution(Object testInstance, Method testMethod) throws E
364
359
* @see #getTestExecutionListeners()
365
360
* @see Throwable#addSuppressed(Throwable)
366
361
*/
367
- public void afterTestExecution (Object testInstance , Method testMethod , @ Nullable Throwable exception ) throws Exception {
362
+ public void afterTestExecution (Object testInstance , Method testMethod , @ Nullable Throwable exception )
363
+ throws Exception {
364
+
368
365
String callbackName = "afterTestExecution" ;
369
366
prepareForAfterCallback (callbackName , testInstance , testMethod , exception );
370
-
371
367
Throwable afterTestExecutionException = null ;
368
+
372
369
// Traverse the TestExecutionListeners in reverse order to ensure proper
373
370
// "wrapper"-style execution of listeners.
374
371
for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners ()) {
@@ -385,6 +382,7 @@ public void afterTestExecution(Object testInstance, Method testMethod, @Nullable
385
382
}
386
383
}
387
384
}
385
+
388
386
if (afterTestExecutionException != null ) {
389
387
ReflectionUtils .rethrowException (afterTestExecutionException );
390
388
}
@@ -414,21 +412,22 @@ public void afterTestExecution(Object testInstance, Method testMethod, @Nullable
414
412
* @param testInstance the current test instance (never {@code null})
415
413
* @param testMethod the test method which has just been executed on the
416
414
* test instance
417
- * @param exception the exception that was thrown during execution of the
418
- * test method or by a TestExecutionListener, or {@code null} if none
419
- * was thrown
415
+ * @param exception the exception that was thrown during execution of the test
416
+ * method or by a TestExecutionListener, or {@code null} if none was thrown
420
417
* @throws Exception if a registered TestExecutionListener throws an exception
421
418
* @see #beforeTestMethod
422
419
* @see #beforeTestExecution
423
420
* @see #afterTestExecution
424
421
* @see #getTestExecutionListeners()
425
422
* @see Throwable#addSuppressed(Throwable)
426
423
*/
427
- public void afterTestMethod (Object testInstance , Method testMethod , @ Nullable Throwable exception ) throws Exception {
424
+ public void afterTestMethod (Object testInstance , Method testMethod , @ Nullable Throwable exception )
425
+ throws Exception {
426
+
428
427
String callbackName = "afterTestMethod" ;
429
428
prepareForAfterCallback (callbackName , testInstance , testMethod , exception );
430
-
431
429
Throwable afterTestMethodException = null ;
430
+
432
431
// Traverse the TestExecutionListeners in reverse order to ensure proper
433
432
// "wrapper"-style execution of listeners.
434
433
for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners ()) {
@@ -445,6 +444,7 @@ public void afterTestMethod(Object testInstance, Method testMethod, @Nullable Th
445
444
}
446
445
}
447
446
}
447
+
448
448
if (afterTestMethodException != null ) {
449
449
ReflectionUtils .rethrowException (afterTestMethodException );
450
450
}
@@ -510,33 +510,36 @@ private void prepareForAfterCallback(String callbackName, Object testInstance, M
510
510
@ Nullable Throwable exception ) {
511
511
512
512
if (logger .isTraceEnabled ()) {
513
- logger .trace (String .format ("%s(): instance [%s], method [%s], exception [%s]" , callbackName , testInstance ,
514
- testMethod , exception ));
513
+ logger .trace (String .format ("%s(): instance [%s], method [%s], exception [%s]" ,
514
+ callbackName , testInstance , testMethod , exception ));
515
515
}
516
516
getTestContext ().updateState (testInstance , testMethod , exception );
517
517
}
518
518
519
519
private void handleBeforeException (Throwable ex , String callbackName , TestExecutionListener testExecutionListener ,
520
520
Object testInstance , Method testMethod ) throws Exception {
521
+
521
522
logException (ex , callbackName , testExecutionListener , testInstance , testMethod );
522
523
ReflectionUtils .rethrowException (ex );
523
524
}
524
525
525
- private void logException (Throwable ex , String callbackName , TestExecutionListener testExecutionListener ,
526
- Class <?> testClass ) {
526
+ private void logException (
527
+ Throwable ex , String callbackName , TestExecutionListener testExecutionListener , Class <?> testClass ) {
528
+
527
529
if (logger .isWarnEnabled ()) {
528
530
logger .warn (String .format ("Caught exception while invoking '%s' callback on " +
529
- "TestExecutionListener [%s] for test class [%s]" , callbackName , testExecutionListener ,
530
- testClass ), ex );
531
+ "TestExecutionListener [%s] for test class [%s]" , callbackName , testExecutionListener ,
532
+ testClass ), ex );
531
533
}
532
534
}
533
535
534
536
private void logException (Throwable ex , String callbackName , TestExecutionListener testExecutionListener ,
535
537
Object testInstance , Method testMethod ) {
538
+
536
539
if (logger .isWarnEnabled ()) {
537
540
logger .warn (String .format ("Caught exception while invoking '%s' callback on " +
538
- "TestExecutionListener [%s] for test method [%s] and test instance [%s]" ,
539
- callbackName , testExecutionListener , testMethod , testInstance ), ex );
541
+ "TestExecutionListener [%s] for test method [%s] and test instance [%s]" ,
542
+ callbackName , testExecutionListener , testMethod , testInstance ), ex );
540
543
}
541
544
}
542
545
@@ -546,8 +549,8 @@ private void logException(Throwable ex, String callbackName, TestExecutionListen
546
549
* <em>copy constructor</em>.
547
550
*/
548
551
private static TestContext copyTestContext (TestContext testContext ) {
549
- Constructor <? extends TestContext > constructor = ClassUtils . getConstructorIfAvailable ( testContext . getClass (),
550
- testContext .getClass ());
552
+ Constructor <? extends TestContext > constructor =
553
+ ClassUtils . getConstructorIfAvailable ( testContext . getClass (), testContext .getClass ());
551
554
552
555
if (constructor != null ) {
553
556
try {
@@ -563,7 +566,7 @@ private static TestContext copyTestContext(TestContext testContext) {
563
566
}
564
567
}
565
568
566
- // fallback to original instance
569
+ // Fallback to original instance
567
570
return testContext ;
568
571
}
569
572
0 commit comments