27
27
import org .apache .commons .logging .Log ;
28
28
import org .apache .commons .logging .LogFactory ;
29
29
30
+ import org .springframework .beans .BeanInstantiationException ;
30
31
import org .springframework .beans .BeanUtils ;
31
32
import org .springframework .core .annotation .AnnotationAttributes ;
32
33
import org .springframework .util .Assert ;
74
75
public class TestContextManager {
75
76
76
77
private static final String [] DEFAULT_TEST_EXECUTION_LISTENER_CLASS_NAMES = new String [] {
77
- "org.springframework.test.context.web.ServletTestExecutionListener" ,
78
- "org.springframework.test.context.support.DependencyInjectionTestExecutionListener" ,
79
- "org.springframework.test.context.support.DirtiesContextTestExecutionListener" ,
80
- "org.springframework.test.context.transaction.TransactionalTestExecutionListener" };
78
+ "org.springframework.test.context.web.ServletTestExecutionListener" ,
79
+ "org.springframework.test.context.support.DependencyInjectionTestExecutionListener" ,
80
+ "org.springframework.test.context.support.DirtiesContextTestExecutionListener" ,
81
+ "org.springframework.test.context.transaction.TransactionalTestExecutionListener" };
81
82
82
83
private static final Log logger = LogFactory .getLog (TestContextManager .class );
83
84
@@ -194,24 +195,21 @@ private TestExecutionListener[] retrieveTestExecutionListeners(Class<?> clazz) {
194
195
// Traverse the class hierarchy...
195
196
while (descriptor != null ) {
196
197
Class <?> declaringClass = descriptor .getDeclaringClass ();
197
-
198
198
AnnotationAttributes annAttrs = descriptor .getAnnotationAttributes ();
199
199
if (logger .isTraceEnabled ()) {
200
- logger .trace (String .format (
201
- "Retrieved @TestExecutionListeners attributes [%s] for declaring class [%s]." , annAttrs ,
202
- declaringClass ));
200
+ logger .trace (String .format ("Retrieved @TestExecutionListeners attributes [%s] for declaring class [%s]." ,
201
+ annAttrs , declaringClass ));
203
202
}
204
203
205
- Class <? extends TestExecutionListener >[] valueListenerClasses = (Class <? extends TestExecutionListener >[]) annAttrs .getClassArray ("value" );
206
- Class <? extends TestExecutionListener >[] listenerClasses = (Class <? extends TestExecutionListener >[]) annAttrs .getClassArray ("listeners" );
204
+ Class <? extends TestExecutionListener >[] valueListenerClasses =
205
+ (Class <? extends TestExecutionListener >[]) annAttrs .getClassArray ("value" );
206
+ Class <? extends TestExecutionListener >[] listenerClasses =
207
+ (Class <? extends TestExecutionListener >[]) annAttrs .getClassArray ("listeners" );
207
208
if (!ObjectUtils .isEmpty (valueListenerClasses ) && !ObjectUtils .isEmpty (listenerClasses )) {
208
- String msg = String .format (
209
- "Class [%s] has been configured with @TestExecutionListeners' 'value' [%s] "
210
- + "and 'listeners' [%s] attributes. Use one or the other, but not both." ,
211
- declaringClass , ObjectUtils .nullSafeToString (valueListenerClasses ),
212
- ObjectUtils .nullSafeToString (listenerClasses ));
213
- logger .error (msg );
214
- throw new IllegalStateException (msg );
209
+ throw new IllegalStateException (String .format ("Class [%s] configured with @TestExecutionListeners' " +
210
+ "'value' [%s] and 'listeners' [%s] attributes. Use one or the other, but not both." ,
211
+ declaringClass , ObjectUtils .nullSafeToString (valueListenerClasses ),
212
+ ObjectUtils .nullSafeToString (listenerClasses )));
215
213
}
216
214
else if (!ObjectUtils .isEmpty (valueListenerClasses )) {
217
215
listenerClasses = valueListenerClasses ;
@@ -220,22 +218,31 @@ else if (!ObjectUtils.isEmpty(valueListenerClasses)) {
220
218
if (listenerClasses != null ) {
221
219
classesList .addAll (0 , Arrays .<Class <? extends TestExecutionListener >> asList (listenerClasses ));
222
220
}
223
-
224
221
descriptor = (annAttrs .getBoolean ("inheritListeners" ) ? MetaAnnotationUtils .findAnnotationDescriptor (
225
222
descriptor .getRootDeclaringClass ().getSuperclass (), annotationType ) : null );
226
223
}
227
224
}
228
225
229
226
List <TestExecutionListener > listeners = new ArrayList <TestExecutionListener >(classesList .size ());
230
227
for (Class <? extends TestExecutionListener > listenerClass : classesList ) {
228
+ NoClassDefFoundError ncdfe = null ;
231
229
try {
232
230
listeners .add (BeanUtils .instantiateClass (listenerClass ));
233
231
}
234
232
catch (NoClassDefFoundError err ) {
233
+ ncdfe = err ;
234
+ }
235
+ catch (BeanInstantiationException ex ) {
236
+ if (ex .getCause () instanceof NoClassDefFoundError ) {
237
+ ncdfe = (NoClassDefFoundError ) ex .getCause ();
238
+ }
239
+ }
240
+ if (ncdfe != null ) {
235
241
if (logger .isInfoEnabled ()) {
236
- logger .info (String .format ("Could not instantiate TestExecutionListener class [%s]. " +
242
+ logger .info (String .format ("Could not instantiate TestExecutionListener [%s]. " +
237
243
"Specify custom listener classes or make the default listener classes " +
238
- "(and their dependencies) available." , listenerClass .getName ()));
244
+ "(and their required dependencies) available. Offending class: [%s]" ,
245
+ listenerClass .getName (), ncdfe .getMessage ()));
239
246
}
240
247
}
241
248
}
0 commit comments