From 33d33802a848a2a341f263c07d5cc345f37953a2 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 26 Nov 2022 15:30:50 +0100 Subject: [PATCH] Improve logging in TestContextManager --- .../test/context/TestContextManager.java | 48 ++++++++++++------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/TestContextManager.java b/spring-test/src/main/java/org/springframework/test/context/TestContextManager.java index ebd546dc547e..a61461a8928b 100644 --- a/spring-test/src/main/java/org/springframework/test/context/TestContextManager.java +++ b/spring-test/src/main/java/org/springframework/test/context/TestContextManager.java @@ -162,7 +162,7 @@ public void registerTestExecutionListeners(List testExecu public void registerTestExecutionListeners(TestExecutionListener... testExecutionListeners) { for (TestExecutionListener listener : testExecutionListeners) { if (logger.isTraceEnabled()) { - logger.trace("Registering TestExecutionListener: " + listener); + logger.trace("Registering TestExecutionListener: " + typeName(listener)); } this.testExecutionListeners.add(listener); } @@ -205,7 +205,7 @@ private List getReversedTestExecutionListeners() { public void beforeTestClass() throws Exception { Class testClass = getTestContext().getTestClass(); if (logger.isTraceEnabled()) { - logger.trace("beforeTestClass(): class [" + testClass.getName() + "]"); + logger.trace("beforeTestClass(): class [" + typeName(testClass) + "]"); } getTestContext().updateState(null, null, null); @@ -250,8 +250,10 @@ public void prepareTestInstance(Object testInstance) throws Exception { } catch (Throwable ex) { if (logger.isErrorEnabled()) { - logger.error("Caught exception while allowing TestExecutionListener [" + testExecutionListener + - "] to prepare test instance [" + testInstance + "]", ex); + logger.error(""" + Caught exception while allowing TestExecutionListener [%s] to \ + prepare test instance [%s]""" + .formatted(typeName(testExecutionListener), testInstance), ex); } ReflectionUtils.rethrowException(ex); } @@ -481,7 +483,7 @@ public void afterTestMethod(Object testInstance, Method testMethod, @Nullable Th public void afterTestClass() throws Exception { Class testClass = getTestContext().getTestClass(); if (logger.isTraceEnabled()) { - logger.trace("afterTestClass(): class [" + testClass.getName() + "]"); + logger.trace("afterTestClass(): class [" + typeName(testClass) + "]"); } getTestContext().updateState(null, null, null); @@ -512,7 +514,7 @@ public void afterTestClass() throws Exception { private void prepareForBeforeCallback(String callbackName, Object testInstance, Method testMethod) { if (logger.isTraceEnabled()) { - logger.trace(String.format("%s(): instance [%s], method [%s]", callbackName, testInstance, testMethod)); + logger.trace("%s(): instance [%s], method [%s]".formatted(callbackName, testInstance, testMethod)); } getTestContext().updateState(testInstance, testMethod, null); } @@ -521,8 +523,8 @@ private void prepareForAfterCallback(String callbackName, Object testInstance, M @Nullable Throwable exception) { if (logger.isTraceEnabled()) { - logger.trace(String.format("%s(): instance [%s], method [%s], exception [%s]", - callbackName, testInstance, testMethod, exception)); + logger.trace("%s(): instance [%s], method [%s], exception [%s]" + .formatted(callbackName, testInstance, testMethod, exception)); } getTestContext().updateState(testInstance, testMethod, exception); } @@ -538,9 +540,10 @@ private void logException( Throwable ex, String callbackName, TestExecutionListener testExecutionListener, Class testClass) { if (logger.isWarnEnabled()) { - logger.warn(String.format("Caught exception while invoking '%s' callback on " + - "TestExecutionListener [%s] for test class [%s]", callbackName, testExecutionListener, - testClass), ex); + logger.warn(""" + Caught exception while invoking '%s' callback on TestExecutionListener [%s] \ + for test class [%s]""" + .formatted(callbackName, typeName(testExecutionListener), typeName(testClass)), ex); } } @@ -548,9 +551,10 @@ private void logException(Throwable ex, String callbackName, TestExecutionListen Object testInstance, Method testMethod) { if (logger.isWarnEnabled()) { - logger.warn(String.format("Caught exception while invoking '%s' callback on " + - "TestExecutionListener [%s] for test method [%s] and test instance [%s]", - callbackName, testExecutionListener, testMethod, testInstance), ex); + logger.warn(""" + Caught exception while invoking '%s' callback on TestExecutionListener [%s] for \ + test method [%s] and test instance [%s]""" + .formatted(callbackName, typeName(testExecutionListener), testMethod, testInstance), ex); } } @@ -570,9 +574,9 @@ private static TestContext copyTestContext(TestContext testContext) { } catch (Exception ex) { if (logger.isInfoEnabled()) { - logger.info(String.format("Failed to invoke copy constructor for [%s]; " + - "concurrent test execution is therefore likely not supported.", - testContext), ex); + logger.info(""" + Failed to invoke copy constructor for [%s]; concurrent test execution \ + is therefore likely not supported.""".formatted(testContext), ex); } } } @@ -581,4 +585,14 @@ private static TestContext copyTestContext(TestContext testContext) { return testContext; } + private static String typeName(Object obj) { + if (obj == null) { + return "null"; + } + if (obj instanceof Class type) { + return type.getName(); + } + return obj.getClass().getName(); + } + }