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 78aaf22402b..43810da35a6 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 @@ -27,6 +27,7 @@ import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.beans.BeanInstantiationException; import org.springframework.beans.BeanUtils; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.util.Assert; @@ -74,10 +75,10 @@ import org.springframework.util.ObjectUtils; public class TestContextManager { private static final String[] DEFAULT_TEST_EXECUTION_LISTENER_CLASS_NAMES = new String[] { - "org.springframework.test.context.web.ServletTestExecutionListener", - "org.springframework.test.context.support.DependencyInjectionTestExecutionListener", - "org.springframework.test.context.support.DirtiesContextTestExecutionListener", - "org.springframework.test.context.transaction.TransactionalTestExecutionListener" }; + "org.springframework.test.context.web.ServletTestExecutionListener", + "org.springframework.test.context.support.DependencyInjectionTestExecutionListener", + "org.springframework.test.context.support.DirtiesContextTestExecutionListener", + "org.springframework.test.context.transaction.TransactionalTestExecutionListener" }; private static final Log logger = LogFactory.getLog(TestContextManager.class); @@ -194,24 +195,21 @@ public class TestContextManager { // Traverse the class hierarchy... while (descriptor != null) { Class declaringClass = descriptor.getDeclaringClass(); - AnnotationAttributes annAttrs = descriptor.getAnnotationAttributes(); if (logger.isTraceEnabled()) { - logger.trace(String.format( - "Retrieved @TestExecutionListeners attributes [%s] for declaring class [%s].", annAttrs, - declaringClass)); + logger.trace(String.format("Retrieved @TestExecutionListeners attributes [%s] for declaring class [%s].", + annAttrs, declaringClass)); } - Class[] valueListenerClasses = (Class[]) annAttrs.getClassArray("value"); - Class[] listenerClasses = (Class[]) annAttrs.getClassArray("listeners"); + Class[] valueListenerClasses = + (Class[]) annAttrs.getClassArray("value"); + Class[] listenerClasses = + (Class[]) annAttrs.getClassArray("listeners"); if (!ObjectUtils.isEmpty(valueListenerClasses) && !ObjectUtils.isEmpty(listenerClasses)) { - String msg = String.format( - "Class [%s] has been configured with @TestExecutionListeners' 'value' [%s] " - + "and 'listeners' [%s] attributes. Use one or the other, but not both.", - declaringClass, ObjectUtils.nullSafeToString(valueListenerClasses), - ObjectUtils.nullSafeToString(listenerClasses)); - logger.error(msg); - throw new IllegalStateException(msg); + throw new IllegalStateException(String.format("Class [%s] configured with @TestExecutionListeners' " + + "'value' [%s] and 'listeners' [%s] attributes. Use one or the other, but not both.", + declaringClass, ObjectUtils.nullSafeToString(valueListenerClasses), + ObjectUtils.nullSafeToString(listenerClasses))); } else if (!ObjectUtils.isEmpty(valueListenerClasses)) { listenerClasses = valueListenerClasses; @@ -220,7 +218,6 @@ public class TestContextManager { if (listenerClasses != null) { classesList.addAll(0, Arrays.> asList(listenerClasses)); } - descriptor = (annAttrs.getBoolean("inheritListeners") ? MetaAnnotationUtils.findAnnotationDescriptor( descriptor.getRootDeclaringClass().getSuperclass(), annotationType) : null); } @@ -228,14 +225,24 @@ public class TestContextManager { List listeners = new ArrayList(classesList.size()); for (Class listenerClass : classesList) { + NoClassDefFoundError ncdfe = null; try { listeners.add(BeanUtils.instantiateClass(listenerClass)); } catch (NoClassDefFoundError err) { + ncdfe = err; + } + catch (BeanInstantiationException ex) { + if (ex.getCause() instanceof NoClassDefFoundError) { + ncdfe = (NoClassDefFoundError) ex.getCause(); + } + } + if (ncdfe != null) { if (logger.isInfoEnabled()) { - logger.info(String.format("Could not instantiate TestExecutionListener class [%s]. " + + logger.info(String.format("Could not instantiate TestExecutionListener [%s]. " + "Specify custom listener classes or make the default listener classes " + - "(and their dependencies) available.", listenerClass.getName())); + "(and their required dependencies) available. Offending class: [%s]", + listenerClass.getName(), ncdfe.getMessage())); } } }