@@ -94,13 +94,8 @@ public class TestContextManager {
9494
9595 private final TestContext testContext ;
9696
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 ));
10499
105100 private final List <TestExecutionListener > testExecutionListeners = new ArrayList <>();
106101
@@ -364,11 +359,13 @@ public void beforeTestExecution(Object testInstance, Method testMethod) throws E
364359 * @see #getTestExecutionListeners()
365360 * @see Throwable#addSuppressed(Throwable)
366361 */
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+
368365 String callbackName = "afterTestExecution" ;
369366 prepareForAfterCallback (callbackName , testInstance , testMethod , exception );
370-
371367 Throwable afterTestExecutionException = null ;
368+
372369 // Traverse the TestExecutionListeners in reverse order to ensure proper
373370 // "wrapper"-style execution of listeners.
374371 for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners ()) {
@@ -385,6 +382,7 @@ public void afterTestExecution(Object testInstance, Method testMethod, @Nullable
385382 }
386383 }
387384 }
385+
388386 if (afterTestExecutionException != null ) {
389387 ReflectionUtils .rethrowException (afterTestExecutionException );
390388 }
@@ -414,21 +412,22 @@ public void afterTestExecution(Object testInstance, Method testMethod, @Nullable
414412 * @param testInstance the current test instance (never {@code null})
415413 * @param testMethod the test method which has just been executed on the
416414 * 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
420417 * @throws Exception if a registered TestExecutionListener throws an exception
421418 * @see #beforeTestMethod
422419 * @see #beforeTestExecution
423420 * @see #afterTestExecution
424421 * @see #getTestExecutionListeners()
425422 * @see Throwable#addSuppressed(Throwable)
426423 */
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+
428427 String callbackName = "afterTestMethod" ;
429428 prepareForAfterCallback (callbackName , testInstance , testMethod , exception );
430-
431429 Throwable afterTestMethodException = null ;
430+
432431 // Traverse the TestExecutionListeners in reverse order to ensure proper
433432 // "wrapper"-style execution of listeners.
434433 for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners ()) {
@@ -445,6 +444,7 @@ public void afterTestMethod(Object testInstance, Method testMethod, @Nullable Th
445444 }
446445 }
447446 }
447+
448448 if (afterTestMethodException != null ) {
449449 ReflectionUtils .rethrowException (afterTestMethodException );
450450 }
@@ -510,33 +510,36 @@ private void prepareForAfterCallback(String callbackName, Object testInstance, M
510510 @ Nullable Throwable exception ) {
511511
512512 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 ));
515515 }
516516 getTestContext ().updateState (testInstance , testMethod , exception );
517517 }
518518
519519 private void handleBeforeException (Throwable ex , String callbackName , TestExecutionListener testExecutionListener ,
520520 Object testInstance , Method testMethod ) throws Exception {
521+
521522 logException (ex , callbackName , testExecutionListener , testInstance , testMethod );
522523 ReflectionUtils .rethrowException (ex );
523524 }
524525
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+
527529 if (logger .isWarnEnabled ()) {
528530 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 );
531533 }
532534 }
533535
534536 private void logException (Throwable ex , String callbackName , TestExecutionListener testExecutionListener ,
535537 Object testInstance , Method testMethod ) {
538+
536539 if (logger .isWarnEnabled ()) {
537540 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 );
540543 }
541544 }
542545
@@ -546,8 +549,8 @@ private void logException(Throwable ex, String callbackName, TestExecutionListen
546549 * <em>copy constructor</em>.
547550 */
548551 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 ());
551554
552555 if (constructor != null ) {
553556 try {
@@ -563,7 +566,7 @@ private static TestContext copyTestContext(TestContext testContext) {
563566 }
564567 }
565568
566- // fallback to original instance
569+ // Fallback to original instance
567570 return testContext ;
568571 }
569572
0 commit comments