Improve logging in TestContextManager

This commit is contained in:
Sam Brannen 2022-11-26 15:30:50 +01:00
parent 9ec937c843
commit 33d33802a8
1 changed files with 31 additions and 17 deletions

View File

@ -162,7 +162,7 @@ public class TestContextManager {
public void registerTestExecutionListeners(TestExecutionListener... testExecutionListeners) { public void registerTestExecutionListeners(TestExecutionListener... testExecutionListeners) {
for (TestExecutionListener listener : testExecutionListeners) { for (TestExecutionListener listener : testExecutionListeners) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Registering TestExecutionListener: " + listener); logger.trace("Registering TestExecutionListener: " + typeName(listener));
} }
this.testExecutionListeners.add(listener); this.testExecutionListeners.add(listener);
} }
@ -205,7 +205,7 @@ public class TestContextManager {
public void beforeTestClass() throws Exception { public void beforeTestClass() throws Exception {
Class<?> testClass = getTestContext().getTestClass(); Class<?> testClass = getTestContext().getTestClass();
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("beforeTestClass(): class [" + testClass.getName() + "]"); logger.trace("beforeTestClass(): class [" + typeName(testClass) + "]");
} }
getTestContext().updateState(null, null, null); getTestContext().updateState(null, null, null);
@ -250,8 +250,10 @@ public class TestContextManager {
} }
catch (Throwable ex) { catch (Throwable ex) {
if (logger.isErrorEnabled()) { if (logger.isErrorEnabled()) {
logger.error("Caught exception while allowing TestExecutionListener [" + testExecutionListener + logger.error("""
"] to prepare test instance [" + testInstance + "]", ex); Caught exception while allowing TestExecutionListener [%s] to \
prepare test instance [%s]"""
.formatted(typeName(testExecutionListener), testInstance), ex);
} }
ReflectionUtils.rethrowException(ex); ReflectionUtils.rethrowException(ex);
} }
@ -481,7 +483,7 @@ public class TestContextManager {
public void afterTestClass() throws Exception { public void afterTestClass() throws Exception {
Class<?> testClass = getTestContext().getTestClass(); Class<?> testClass = getTestContext().getTestClass();
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("afterTestClass(): class [" + testClass.getName() + "]"); logger.trace("afterTestClass(): class [" + typeName(testClass) + "]");
} }
getTestContext().updateState(null, null, null); getTestContext().updateState(null, null, null);
@ -512,7 +514,7 @@ public class TestContextManager {
private void prepareForBeforeCallback(String callbackName, Object testInstance, Method testMethod) { private void prepareForBeforeCallback(String callbackName, Object testInstance, Method testMethod) {
if (logger.isTraceEnabled()) { 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); getTestContext().updateState(testInstance, testMethod, null);
} }
@ -521,8 +523,8 @@ public class TestContextManager {
@Nullable Throwable exception) { @Nullable Throwable exception) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace(String.format("%s(): instance [%s], method [%s], exception [%s]", logger.trace("%s(): instance [%s], method [%s], exception [%s]"
callbackName, testInstance, testMethod, exception)); .formatted(callbackName, testInstance, testMethod, exception));
} }
getTestContext().updateState(testInstance, testMethod, exception); getTestContext().updateState(testInstance, testMethod, exception);
} }
@ -538,9 +540,10 @@ public class TestContextManager {
Throwable ex, String callbackName, TestExecutionListener testExecutionListener, Class<?> testClass) { Throwable ex, String callbackName, TestExecutionListener testExecutionListener, Class<?> testClass) {
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {
logger.warn(String.format("Caught exception while invoking '%s' callback on " + logger.warn("""
"TestExecutionListener [%s] for test class [%s]", callbackName, testExecutionListener, Caught exception while invoking '%s' callback on TestExecutionListener [%s] \
testClass), ex); for test class [%s]"""
.formatted(callbackName, typeName(testExecutionListener), typeName(testClass)), ex);
} }
} }
@ -548,9 +551,10 @@ public class TestContextManager {
Object testInstance, Method testMethod) { Object testInstance, Method testMethod) {
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {
logger.warn(String.format("Caught exception while invoking '%s' callback on " + logger.warn("""
"TestExecutionListener [%s] for test method [%s] and test instance [%s]", Caught exception while invoking '%s' callback on TestExecutionListener [%s] for \
callbackName, testExecutionListener, testMethod, testInstance), ex); test method [%s] and test instance [%s]"""
.formatted(callbackName, typeName(testExecutionListener), testMethod, testInstance), ex);
} }
} }
@ -570,9 +574,9 @@ public class TestContextManager {
} }
catch (Exception ex) { catch (Exception ex) {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info(String.format("Failed to invoke copy constructor for [%s]; " + logger.info("""
"concurrent test execution is therefore likely not supported.", Failed to invoke copy constructor for [%s]; concurrent test execution \
testContext), ex); is therefore likely not supported.""".formatted(testContext), ex);
} }
} }
} }
@ -581,4 +585,14 @@ public class TestContextManager {
return 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();
}
} }