Skip to content

Commit 09b1e5e

Browse files
committed
Extract instantiateComponents() method in AbstractTestContextBootstrapper
1 parent 5996196 commit 09b1e5e

File tree

1 file changed

+15
-33
lines changed

1 file changed

+15
-33
lines changed

spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ else if (logger.isDebugEnabled()) {
185185
return listeners;
186186
}
187187

188+
@SuppressWarnings("unchecked")
189+
private List<TestExecutionListener> instantiateListeners(Class<? extends TestExecutionListener>... classes) {
190+
return instantiateComponents(TestExecutionListener.class, classes);
191+
}
192+
188193
/**
189194
* Get the default {@link TestExecutionListener TestExecutionListeners} for
190195
* this bootstrapper.
@@ -199,33 +204,6 @@ protected List<TestExecutionListener> getDefaultTestExecutionListeners() {
199204
return TestContextSpringFactoriesUtils.loadFactoryImplementations(TestExecutionListener.class);
200205
}
201206

202-
@SuppressWarnings("unchecked")
203-
private List<TestExecutionListener> instantiateListeners(Class<? extends TestExecutionListener>... classes) {
204-
List<TestExecutionListener> listeners = new ArrayList<>(classes.length);
205-
for (Class<? extends TestExecutionListener> listenerClass : classes) {
206-
try {
207-
listeners.add(BeanUtils.instantiateClass(listenerClass));
208-
}
209-
catch (BeanInstantiationException ex) {
210-
Throwable cause = ex.getCause();
211-
if (cause instanceof ClassNotFoundException || cause instanceof NoClassDefFoundError) {
212-
if (logger.isDebugEnabled()) {
213-
logger.debug("""
214-
Skipping candidate %1$s [%2$s] due to a missing dependency. \
215-
Specify custom %1$s classes or make the default %1$s classes \
216-
and their required dependencies available. Offending class: [%3$s]"""
217-
.formatted(TestExecutionListener.class.getSimpleName(), listenerClass.getName(),
218-
cause.getMessage()));
219-
}
220-
}
221-
else {
222-
throw ex;
223-
}
224-
}
225-
}
226-
return listeners;
227-
}
228-
229207
/**
230208
* {@inheritDoc}
231209
*/
@@ -473,10 +451,15 @@ protected List<ContextCustomizerFactory> getContextCustomizerFactories() {
473451

474452
@SuppressWarnings("unchecked")
475453
private List<ContextCustomizerFactory> instantiateCustomizerFactories(Class<? extends ContextCustomizerFactory>... classes) {
476-
List<ContextCustomizerFactory> factories = new ArrayList<>(classes.length);
477-
for (Class<? extends ContextCustomizerFactory> factoryClass : classes) {
454+
return instantiateComponents(ContextCustomizerFactory.class, classes);
455+
}
456+
457+
@SuppressWarnings("unchecked")
458+
private <T> List<T> instantiateComponents(Class<T> componentType, Class<? extends T>... classes) {
459+
List<T> components = new ArrayList<>(classes.length);
460+
for (Class<? extends T> clazz : classes) {
478461
try {
479-
factories.add(BeanUtils.instantiateClass(factoryClass));
462+
components.add(BeanUtils.instantiateClass(clazz));
480463
}
481464
catch (BeanInstantiationException ex) {
482465
Throwable cause = ex.getCause();
@@ -486,16 +469,15 @@ private List<ContextCustomizerFactory> instantiateCustomizerFactories(Class<? ex
486469
Skipping candidate %1$s [%2$s] due to a missing dependency. \
487470
Specify custom %1$s classes or make the default %1$s classes \
488471
and their required dependencies available. Offending class: [%3$s]"""
489-
.formatted(ContextCustomizerFactory.class.getSimpleName(), factoryClass.getName(),
490-
cause.getMessage()));
472+
.formatted(componentType.getSimpleName(), clazz.getName(), cause.getMessage()));
491473
}
492474
}
493475
else {
494476
throw ex;
495477
}
496478
}
497479
}
498-
return factories;
480+
return components;
499481
}
500482

501483
/**

0 commit comments

Comments
 (0)